Googlebot
Googlebot

Reputation: 15673

Resizing image with PHP

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?

Upvotes: 0

Views: 992

Answers (2)

Thein Hla Maw
Thein Hla Maw

Reputation: 683

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

Use Asido: PHP Image Processing Solution

Asido supports the following features:

  • pluggable drivers for GD2 (php_gd2), MagickWand (php_magickwand), ImageMagick extension (php_imagick) as well as ImageMagick shell commands
  • "hack" drivers: workarounds for certain disabilities of a particular driver by using some of the other functionality provided by the environment
  • various resize functionality: proportional resize, resize only by width or height, stretch resize, fit resize, frame resize
  • watermark images, including tiling watermark and automatic scaling of large watermarks
  • rotate images
  • copy images onto one another
  • crop images
  • grayscale images
  • convert images between different filetypes

If you can't access Asido website, you can download Asido from SourceForge.net

Upvotes: 2

Laur Ivan
Laur Ivan

Reputation: 4177

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.

Upvotes: 2

Related Questions