Resize the image with PHP

advertisements

I used a very simple code to resize image with PHP; but surprisingly it does not work for some images. The problem should be associated with imagecreatefromjpeg(), as it will generate a black image (which is of the background image).

$picture="test5.jpg";
$url="http://www.pokerpurist.com/uploadedImages/bettingpro/NewsImages/TN98553_Perla-Beltran.jpg";
list($width, $height) = getimagesize($url);

$new_height = $height / $width * 400;
$image_p = imagecreatetruecolor(400, $new_height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height);
imagejpeg($image_p, $picture);
echo "<img src='$picture' />";

This problem happens offen, and I included an example image. What is the problem with these images leading to this problem? It seems to be a normal JPG image.

By the way, is it the simplest and most efficient way to resize image with PHP/GD2?


Your example image is a PNG, not JPEG. You probably need to put some detection code in place...

Edit: exif-imagetype or ImageMagick might be of some use.