How do you download, resize and store an image from a remote server using php?
This is the code I am using
$temp_image = file_get_contents($url);
$image = imagecreatefromstring($temp_image);
$thumb = imageToCanvas($image,100,75,true);
imagejpeg($thumb,$base_image_path . $thumb_path,90)
function imageToCanvas($_image, $_canvasWidth, $_canvasHeight, $forceScale=false,$x=false,$y=false)
{
$newImage = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
$imageinfo = getimagesize($_image);
$sourceWidth = $imageinfo[0];
$sourceHeight = $imageinfo[1];
$sourceImage = openImage($_image);
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $_canvasWidth, $_canvasHeight, $sourceWidth, $sourceHeight);
return $newImage;
}
function openImage($file)
{
// *** Get extension
$extension = strtolower(strrchr($file, '.'));
switch($extension) {
case '.jpg': case '.jpeg':
$img = @imagecreatefromjpeg($file);
break;
case '.gif':
$img = @imagecreatefromgif($file);
break;
case '.png':
$img = @imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
Doesn’t work and I don’t know why.
$sourceWidth & $sourceHeight doesn’t have a value so I presume $image is in the wrong format
Thanks!
There is no function in php called
openImageso that would be a problem if you don’t define it yourself.If you do have it defined, what does it look like and are you receiving any errors?
Edit: Based on your comments the problem would seem to be that you treat the input parameter of your
openImagefunction as a file path. However, when you call it you are feeding it an image resource, the result ofimagecreatefromstring.