PHP Image resize - Why is the image uploaded but not resized?
- by Hans
BACKGROUND
I have a script to upload an image. One to keep the original image and one to resize the image.
1. If the image dimensions (width & height) are within max dimensions I use a simple "copy" direct to folder UserPics.
2. If the original dimensions are bigger than max dimensions I want to resize the width and height to be within max.
Both of them are uploading the image to the folder, but in case 2, the image will not be resized.
QUESTION
Is there something wrong with the script?
Is there something wrong with the settings?
SETTINGS
Server: WAMP 2.0
PHP: 5.3.0
PHP.ini: GD2 enabled, Memory=128M (have tried 1000M)
Tried imagetypes uploaded: jpg, jpeg, gif, and png (same result for all of them)
SCRIPT
//Uploaded image
$filename = stripslashes($_FILES['file']['name']);
//Read filetype
$i = strrpos($filename,".");
if (!$i) { return ""; }
$l = strlen($filename) - $i;
$extension = substr($filename,$i+1,$l);
$extension = strtolower($extension);
//New picture name = maxid+1 (from database)
$query = mysql_query("SELECT MAX(PicId) AS number FROM userpictures");
$row = mysql_fetch_array($query);
$imagenumber = $row['number']+1;
//New name of image (including path)
$image_name=$imagenumber.'.'.$extension;
$newname = "UserPics/".$image_name;
//Check width and height of uploaded image
list($width,$height)=getimagesize($uploadedfile);
//Check memory to hold this image (added only as checkup)
$imageInfo = getimagesize($uploadedfile);
$requiredMemoryMB = ( $imageInfo[0] * $imageInfo[1] * ($imageInfo['bits'] / 8) * $imageInfo['channels'] * 2.5 ) / 1024;
echo $requiredMemoryMB."<br>";
//Max dimensions that can be uploaded
$maxwidth = 20;
$maxheight = 20;
// Check if dimensions shall be original
if ($width > $maxwidth || $height > $maxheight) {
//Make jpeg from uploaded image
if ($extension=="jpg" || $extension=="jpeg" || $extension=="pjpeg" ) {
$modifiedimage = imagecreatefromjpeg($uploadedfile);
} elseif ($extension=="png") {
$modifiedimage = imagecreatefrompng($uploadedfile);
} elseif ($extension=="gif") {
$modifiedimage = imagecreatefromgif($uploadedfile);
}
//Change dimensions
if ($width > $height) {
$newwidth = $maxwidth;
$newheight = ($height/$width)*$newwidth;
} else {
$newheight = $maxheight;
$newwidth = ($width/$height)*$newheight;
}
//Create new image with new dimensions
$newdim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresized($newdim,$modifiedimage,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($modifiedimage,$newname,60);
// Remove temp images
imagedestroy($modifiedimage);
imagedestroy($newdim);
} else {
// Just add picture to folder without resize (if org dim < max dim)
$newwidth = $width;
$newheight = $height;
$copied = copy($_FILES['file']['tmp_name'], $newname);
}
//Add image information to the MySQL database
mysql_query("SET character_set_connection=utf8", $dbh);
mysql_query("INSERT INTO userpictures (PicId, Picext, UserId, Width, Height, Size) VALUES('$imagenumber', '$extension', '$_SESSION[userid]', '$newwidth', '$newheight', $size)")