Reputation: 23
I created a script that resizes images based on their ratio. For some reason, it keeps echoing "Failed" and is not moving the resized thumbnail to the directory I have set. The directory is a valid path, so I must have done something wrong when creating an image with PHP.
$set_width = $params['width'];
$set_height = $params['height'];
if($filetype == "image/pjpeg" || $filetype == "image/jpeg") {
$new_image = imagecreatefromjpeg($filetmp);
} elseif($filetype == "image/x-png" || $filetype == "image/png") {
$new_image = imagecreatefrompng($filetmp);
} elseif($filetype == "image/gif") {
$new_image = imagecreatefromgif($filetmp);
}
list($width, $height) = getimagesize($filetmp);
$ratio = $width / $height;
if($width != $set_width || $height != $set_height) {
if($ratio > 1)
{
$set_height = $set_width / $ratio;
}
else
{
$set_height = $set_width;
$set_width = $set_width * $ratio;
}
$resized = imagecreatetruecolor($set_width, $set_height);
$rename = $folder . '_' . time() . mt_rand(1,99) . '.' . end($fileext);
imagecopyresampled($resized, $new_image, 0, 0, 0, 0, $set_width, $set_height, $width, $height);
imagejpeg($resized, "$dirpath/$rename", 100);
imageDestroy($resized);
imageDestroy($new_image);
if(move_uploaded_file($resized, "$dirpath/$rename")) {
return $rename;
} else {
echo 'failed';
}
} else {
$rename = $folder . '_' . time() . mt_rand(1,99) . '.' . end($fileext);
if(move_uploaded_file($filetmp, "$dirpath/$rename")) {
return $rename;
} else {
echo 'failed 2';
}
}
Upvotes: 1
Views: 154
Reputation: 8334
You don't move the resource returned by imagecreatetruecolor()
The GD library creates the image in memory, it doesn't alter the file you used in imagecreatefrom*()
You use one of the GD functions to save the image...
imagejpeg
- Output image to browser or fileimagepng
- Output a PNG image to either the browser or a fileimagegif
- Output image to browser or fileetc...
You've used the correct line
imagejpeg($resized, "$dirpath/$rename", 100);
You are trying to save the GD resource $resized
using move_upload_file. $resized
isn't a file name, so the move_uploaded_file()
fails.
You don't need that call at all, the file should already be saved at $dirpath/$rename
by the imagejpeg()
call.
Upvotes: 1
Reputation: 4374
Check the values of set_width
and set_height
in $resized = imagecreatetruecolor($set_width, $set_height);
Maybe its sending a null value
Upvotes: 0
Reputation: 299
You should check permissions of the directory, keep in mind your web server may be running under a different user then you.
http://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions if you need to learn about *nix permissions.
Upvotes: 0