Page 1 of 1

PHP upload resize and GD2

Posted: Tue Nov 15, 2005 2:44 am
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 !

Posted: Tue Nov 15, 2005 4:54 am
by Grim...
Have you got any errors?

Posted: Tue Nov 15, 2005 4:59 am
by php3ch0
There are no errors. All works fine with JPG. Code runs through fine with GIF but no image uploaded.

Thanks in advance

Posted: Tue Nov 15, 2005 5:01 am
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.

Posted: Tue Nov 15, 2005 5:07 am
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.

Posted: Tue Nov 15, 2005 5:10 am
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.

Posted: Tue Nov 15, 2005 9:59 am
by onion2k
Does imagecreatefromgif() definitely work on your server? It's awfully common that it doesn't.

Posted: Tue Nov 15, 2005 11:33 am
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.

Posted: Wed Nov 16, 2005 5:39 am
by php3ch0
I am currently running php 4.2.1. I'll upgrade to latest version and see if this helps.