I have encountered a problem with creating a thumbnail from an uploaded image file, then to upload it to the server.
As of now, I have a function that creates the thumbnail, saves it as a temp and returns it to the caller.
Then what I try to do, is upload that created thumb image with move_uploaded_file(tempthumb, path);
Here is the createThumb function and the caller:
function createThumb( $image, $thumbWidth )
{
// load image and get image size
$img = imagecreatefromjpeg( "{$image}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $tmp_img;
}
return $tmp_img; // Here I return the new image. Is this the proper way to get a binary image back??
Here is the caller:
$thumb = createThumb($_FILES['propform-previmg']['tmp_name'], $max_previmg_width);
$filenamepath = $src_dir . '/thumb/' . $_FILES['propform-previmg']['name'];
if ( !move_uploaded_file($thumb, $filenamepath ))
echo "Error moving file {$filenamepath}";
I tried to just upload the uploaded file directly without trying to make a thumbnail first, and that worked fine. So I guess there is some error with the variable I return from the createThumb function, but I can’t figure out exactly what.
Also, I need to do the upload from the caller code, and not inside the createThumb function with imagejpeg(file, path).
Thank you!
First line from the manual:
Your thumbnail wasn’t uploaded, it even isn’t a file yet, but just an image resource. To write the image, call something like
imagejpeg($resource, $filename)to write it to the path specified in$filename.