PHP upload resize and GD2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

PHP upload resize and GD2

Post by php3ch0 »

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hello everyone and congrats to getting the forum back!

I have a small problem. I have created a script to upload an image from the users computer. It should upload it, then resize the picture (this is because people tend to upload 2Meg + files from their digital cameras) then copy it to a directory.

The problem I am having is that it works fine with JPG images but not GIF. Can anyone see why this might be or suggest how I can change this code to allow it?

Code: Select all

<?php require_once('../../php/page_functions.php'); 
require_once('../../Connections/db_connect.php');
require_once('../../php/conversion_functions.php');
require_once('../../php/download_functions.php');

$error = '0';

// restrict_access(access_level,redirect_URL,Username);
if (!empty($_SERVER['QUERY_STRING'])) {
$url = $PHP_SELF."?".$_SERVER['QUERY_STRING'];
} else {
$url = $PHP_SELF;
}
restrict_access(6,$url,$_SESSION['MM_Username']);
// end restrict_access

function ResizeImage($im,$maxwidth,$maxheight,$name){
	$width = imagesx($im);
	$height = imagesy($im);
	if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
		if($maxwidth && $width > $maxwidth){
			$widthratio = $maxwidth/$width;
			@$RESIZEWIDTH=true;
		}
		if($maxheight && $height > $maxheight){
			$heightratio = $maxheight/$height;
			@$RESIZEHEIGHT=true;
		}
		if(@$RESIZEWIDTH && @$RESIZEHEIGHT){
			if($widthratio < $heightratio){
				$ratio = $widthratio;
			}else{
				$ratio = $heightratio;
			}
		}elseif(@$RESIZEWIDTH){
			$ratio = $widthratio;
		}elseif(@$RESIZEHEIGHT){
			$ratio = $heightratio;
		}
    	$newwidth = $width * $ratio;
        $newheight = $height * $ratio;
		if(function_exists("imagecopyresampled")){
      		$newim = imagecreatetruecolor($newwidth, $newheight);
      		imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		}else{
			$newim = imagecreate($newwidth, $newheight);
      		imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		}
        ImageJpeg ($newim,$name . ".jpg");
		ImageDestroy ($newim);
	}else{
		ImageJpeg ($im,$name . ".jpg");
	}
}
$file_name = $HTTP_POST_FILES['file']['name'];
if ($file_name['file']['name'] = "JPG") { $file_name['file']['name'] = "jpg"; }


$type = get_file_type($file_name);
switch ($type) {
	case "jpg":
		break;
	case "JPG":
		break;
	case "jpeg":
		break;
	default:
		$error = "2";
		}
if(empty($_POST['question'])) { $error = '1'; }
		
if ($error == '0') {
//inserting data
mysql_select_db($database_db_connect, $db_connect);
$insert_sql = "insert into quizzes(quiz_id, question_number, question, multi_options, image, max_options) values('$quiz_id', '$question_id', '$question', '$multiple_answers','$file_name', '$max_correct_answers')";
mysql_query($insert_sql, $db_connect) or die(mysql_error());

// begin_new_script;
// Filename to store image as (no extention)
$FILENAME= "../../images/quizzes/quiz/".$quiz_id."/".$file_name;

// Width to reszie image to (in pixels) 
$RESIZEWIDTH=300;

// Width to reszie image to (in pixels) 
$RESIZEHEIGHT=255;

// DO NOT EDIT BELOW HERE ---------------------------------------


if($_FILES['file']['size']){
	if($_FILES['file']['type'] == "image/pjpeg" || $_FILES['file']['type'] == "image/jpeg"){
		$im = imagecreatefromjpeg($_FILES['file']['tmp_name']);
	}elseif($_FILES['file']['type'] == "image/x-png" || $_FILES['file']['type'] == "image/png"){
		$im = imagecreatefrompng($_FILES['file']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/gif"){
		$im = imagecreatefromgif($_FILES['file']['tmp_name']);
	}
	if($im){
		if(file_exists("$FILENAME")){
			unlink("$FILENAME");
		}
    	ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
    	ImageDestroy ($im);
	}
	

} 
$urlstring="add4.php?quiz_id=".$quiz_id."&question_id=".$question_id."&no_of_options=".$no_of_options;
header("Location:$urlstring");
} else { header("Location:add3.php?quiz_id=$quiz_id&error=$error"); }
?>
P.S: Sorry that it is not all commented and well organised yet. I want to get it working first. Pleae dont rip me apart for bad coding pactice !
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Have you got any errors?
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

There are no errors. All works fine with JPG. Code runs through fine with GIF but no image uploaded.

Thanks in advance
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

Sorry I made a change in the code:

Code: Select all

$type = get_file_type($file_name); 
switch ($type) { 
   case "jpg": 
      break; 
   case "JPG": 
      break; 
   case "jpeg": 
      break; 
   default: 
      $error = "2"; 
}
should acctually read:

Code: Select all

$type = get_file_type($file_name); 
switch ($type) { 
   case "jpg": 
      break; 
   case "JPG": 
      break; 
   case "jpeg": 
      break; 
   case "gif": 
      break;
   case "GIF": 
      break;
   default: 
      $error = "2"; 
}
I changed this as I gave up and decided it should only allow JPG but I would like to know why it would not work.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

If you want to save uploaded gifs, I think you have to keep them as gifs.

Although someone will probably tell you differently.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

prehaps if I hav a different fuction for gifs. The origional Idea was to reduce the filesize of JPGs people were uploading from digital cameras

A work around looks like a good solution.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Does imagecreatefromgif() definitely work on your server? It's awfully common that it doesn't.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

onion2k wrote:Does imagecreatefromgif() definitely work on your server? It's awfully common that it doesn't.
My first thought...
Php Manual wrote:Note: GIF support was removed from the GD library in Version 1.6, and added back in Version 2.0.28. This function is not available between these versions.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

I am currently running php 4.2.1. I'll upgrade to latest version and see if this helps.
Post Reply