script - php resize image keep aspect ratio




Resizing with GD outputs black images (3)

Below function creates a thumbnail, with few changes you can O/P it to the screen as well. I think this way it's more useful.

/*
 * PHP function to resize an image maintaining aspect ratio
 * http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
 *
 * Creates a resized (e.g. thumbnail, small, medium, large)
 * version of an image file and saves it as another file
 */

define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);

function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
{
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
    switch ($source_image_type) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($source_image_path);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($source_image_path);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($source_image_path);
            break;
    }
    if ($source_gd_image === false) {
        return false;
    }
    $source_aspect_ratio = $source_image_width / $source_image_height;
    $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
    if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
        $thumbnail_image_width = $source_image_width;
        $thumbnail_image_height = $source_image_height;
    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
        $thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
        $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
    } else {
        $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
        $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
    }
    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
    imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
    imagedestroy($source_gd_image);
    imagedestroy($thumbnail_gd_image);
    return true;
}

Refer this more information.

If php-gd doesn't exist use the following command.

sudo apt-get install php5-gd

What can cause php gd to produce a black image after resizing? The following code always outputs a black image for every valid jpeg file.

<?php

$filename = 'test.jpg';
$percent = 0.5;

header('Content-Type: image/jpeg');

list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);
imagedestroy($thumb);
?>

output of gd_info():

  Array
(
    [GD Version] => bundled (2.1.0 compatible)
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPEG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XPM Support] => 
    [XBM Support] => 1
    [JIS-mapped Japanese Font Support] => 
)

The code appeared working in other environments. Probably it is related to OS, installed packages, libraries, etc?


Ensure gd is installed and enabled.

To check, create a PHP file with this command:

<?php phpinfo(); 

Access the file through a browser and scroll down to the gd section. If gd isn't there, or it is disabled, add it with yum, apt-get or the Windows equivalent.

You also need the GD library available (http://www.libgd.org/).

Consider switching to IMagick (http://php.net/manual/en/book.imagick.php) for better image quality.


Research

Just tried to reproduce your situation. Running your code with out-of-the-box PHP and Apache displays the following

The image “http://localhost/” cannot be displayed because it contains errors.

Although browser tells you that there were some errors, they cannot be seen because of the headers returned in response were of Content-Type: image/jpeg thus forcing browser to interpret it as an image. By removing the header and setting the following will output errors.

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
...
//header('Content-Type: image/jpeg');
...
Answer

What can cause php gd to produce a black image after resizing?

Since the gd_info output proves that the GD extension is loaded, check if the filename (linux is caseSensitive) and permissions are correct. If Apache is running as www-data (group)

sudo chown :www-data test.jpg && sudo chmod 660 test.jpg 
Code improvement / solution
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

if (extension_loaded('gd') && function_exists('gd_info'))
{
    $filename = 'test.jpg';

    if (file_exists($filename) && is_readable($filename))
    {
        $percent = 0.5;

        header('Content-Type: image/jpeg');

        list($width, $height) = getimagesize($filename);
        $newwidth = $width * $percent;
        $newheight = $height * $percent;

        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($filename);

        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        imagejpeg($thumb);
        imagedestroy($thumb);
    }
    else
    {
        trigger_error('File or permission problems');
    }
}
else
{
    trigger_error('GD extension not loaded');
}
Comments

This should be used as a temporary solution (development environment). IMHO, the errors should be handled by a central error handler, display_errors should be false in production. Also, the errors (in this case there would be Fatal error) are logged by default - check the logs for more (the frequent, the better). Also, on linux (with apt) the one-liner will install GD on your system:

sudo apt-get update && sudo apt-get install php5-gd && sudo /etc/init.d/apache2 restart