I’m having a strange (and inconsistent) problem with the two functions detailed below. They are called on submission of a form as follows:
[php]$img_file = $_FILES[“file”];
$main = resize_image($img_file,“upload/”, 500, null);
$thumb = create_thumbnail($img_file,“upload/”);[/font][/php]
The issue is that although both functions actually work, they don’t seem to both work - only the first one called does (in this instance resize_image()). Occasionally, both have worked as expected but not at all consistently. Is there any obvious reason for this? Thanks for any help you are able to offer. Code below:
[php]<?php
//resize_image - 17/11/11
//Function to resize an image file proportionally to it’s largest dimension
//(width or height) and then upload it to a designated path.
//Returns TRUE if successful, FALSE if not.
//Supports GIFs and §JPEGs only:
function resize_image($image_file, $destination_path, $max_long_edge, $filename) {
//if $filename is null - original filename is used:
if ($filename != NULL) {
$image_file[‘name’] = $filename;
}
//Determine the filetype:
$myfiletype = $image_file[“type”];
//Check that file is an image that can be processed:
if ((($myfiletype == “image/gif”)
|| ($myfiletype == “image/jpeg”)
|| ($myfiletype == “image/pjpeg”))
&& ($image_file[“size”] < 1000000))
{
if ($file[“error”] > 0) {
echo "Return Code: " . $file[“error”] . “
”;
return FALSE;
}
else {
//process according to filetype:
if ($myfiletype == "image/gif") {
$src = imagecreatefromgif($image_file["tmp_name"]);
}
if (($myfiletype == "image/jpeg")
|| ($myfiletype == "image/pjpeg")) {
$src = imagecreatefromjpeg($image_file["tmp_name"]);
}
// Capture the original size of the uploaded image
list($width,$height) = getimagesize($image_file["tmp_name"]);
// Change the $newwidth variable to adjust size
if ($width > $height) {
//then image is LANDSCAPE:
$newwidth=$max_long_edge;
$newheight=($height/$width) * $max_long_edge;
}
else {
//then image is PORTRAIT (or SQUARE):
$newheight=$max_long_edge;
$newwidth=($width/$height)*$max_long_edge;
}
//create blank image to copy picture into:
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
//move the resulting file to destination directory:
$filename = $destination_path.$image_file['name'];
if (file_exists($filename)) {
echo $filename." already exists. ";
return FALSE;
}
else {
move_uploaded_file($image_file['tmp_name'],$filename);
if ($myfiletype == "image/gif") {
imagegif($tmp, $filename);
}
else if (($myfiletype == "image/jpeg")
|| ($myfiletype == "image/pjpeg")) {
imagejpeg($tmp, $filename, 100); //Full Quality
}
}
//housekeeping - destroy temporary file:
imagedestroy($tmp);
return TRUE;
}
}
else {
return FALSE;
}
}
//Convenience method to create thumbnail images
//using resize_image method:
function create_thumbnail($image_file, $destination_path) {
$filename = $image_file[‘name’];
//capture file suffix with which to append filename after amending:
$extension = strtolower(substr($filename, strpos($filename, ‘.’) + 1));
//remove file suffix from end of filename
$filename = substr($filename, 0, -4);
//recreate full filename to include suffix: _thumb.jpg:
$filename = $filename."_thumb.".$extension;
//resize image max dimension to 100 pixels:
$i = resize_image($image_file, $destination_path, 100, $filename);
//Will return true of false depending upon whether file successfully uploaded:
return $i;
}
?>[/php]