• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

how to create a thumbnail of a photo?

Hi experts,

I am trying to write a function that creates thumbnails, it works but not for all png photos.

Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'thumbs/sky200.png' is not a valid PNG file

what's wrong with this function? is there a better/robust way to create thumbnails?

thx.

function Create_photo_Thumb($img_path,$thumb_path,$dimension)
{	
	if(strpos($img_path,".gif")) 	$source = imagecreatefromgif($img_path);
	if(strpos($img_path,".png"))	$source = imagecreatefrompng($img_path);
	if(strpos($img_path,".jpg"))	$source = imagecreatefromjpeg($img_path);

	if(!isset($source))
	{	
                return;
	}

	/*====== Get image new sizes ======*/	
		
	list($width, $height) = getimagesize("$img_path");
		
	if($width > $height) { $newwidth  = $dimension; $newheight = $dimension * ($height/$width); }
	else 				 { $newheight = $dimension; $newwidth  = $dimension * ($width/$height); }
		
	/*====== image copy resampled ======*/	
		
	$thumb  = imagecreatetruecolor($newwidth, $newheight);

	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);				
		
	// Output

	imagejpeg($thumb,$thumb_path,99);
}

Open in new window

0
tjome
Asked:
tjome
  • 4
  • 3
1 Solution
 
Ray PaseurCommented:
I don't see anything inherently wrong with the code.  It might just be a bad PNG file.  Can you post a fully-qualified URL to the file?  Thanks.
0
 
tjomeAuthor Commented:
I don't have a url, I am using a local wampserver.

in general how can I check if its a valid photo ?

what about  imagejpeg, is it valid for all type of images?
0
 
Ray PaseurCommented:
ImageJPEG() and ImagePNG() are different functions. These are documented on the PHP.net web site.  They are used to create images from PHP image resources.
http://php.net/manual/en/function.imagejpeg.php
http://php.net/manual/en/function.imagepng.php

The imageCreateFromXXX() functions are documented there, too.

This article tells how I do it.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_6554-Use-PHP-to-Make-an-Image-Thumbnail.html

If you do not have a public facing version of the original file, it may be a little difficult to diagnose what is wrong.  Maybe you can upload the file you have to E-E?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tjomeAuthor Commented:
The following photo causes and error interior-design-wallpaper-1600x1200---Cois there a way to avoid the error message by checking if $img is a valid png/gif/jpg image?
0
 
Ray PaseurCommented:
The image here is a JPEG file.
http://filedb.experts-exchange.com/incoming/2014/07_w29/860872/interior-design-wallpaper-1600x1200---Co

Please see: http://iconoun.com/demo/temp_tjome.php

From the output we can see the image header contains JFIF meaning it should respond to ImageCreateFromJPEG().  The file name suffix is useless here; we need another way to know what kind of image file we have.  That's why I printed out the image header.  In a "real life" script you could test for the presence of "JFIF" in this header and think "JPEG."  Other headers would have other signal strings.  Usually the file name suffix is OK, but in this case we had to go a little further.

<?php // demo/temp_tjome.php
error_reporting(E_ALL);
echo '<pre>';

/**
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28476232.html#a40197616
 * REF http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format
 */

/**
 * Expand and display a variable in hexadecimal notation
 *
 * @param string $str The variable to expand and display
 * @return none (direct browser output)
 */
function hexdump($str, $br=PHP_EOL)
{
    if (empty($str)) return FALSE;

    // GET THE HEX BYTE VALUES IN A STRING
    $hex = str_split(implode(NULL, unpack('H*', $str)));

    // ALLOCATE BYTES INTO HI AND LO NIBBLES
    $hi  = NULL;
    $lo  = NULL;
    $mod = 0;
    foreach ($hex as $nib)
    {
        $mod++;
        $mod = $mod % 2;
        if ($mod)
        {
            $hi .= $nib;
        }
        else
        {
            $lo .= $nib;
        }
    }

    // SHOW THE SCALE, THE STRING AND THE HEX
    $num = substr('1...5...10...15...20...25...30...35...40...45...50...55...60...65...70...75...80...85...90...95..100..105..110..115..120..125..130', 0, strlen($str));
    echo $br . $num;
    echo $br . $str;
    echo $br . $hi;
    echo $br . $lo;
    echo $br;
}

// EXAMINE THE DATA IN THE IMAGE FILE (NAME IS USELESS)
$url = 'http://filedb.experts-exchange.com/incoming/2014/07_w29/860872/interior-design-wallpaper-1600x1200---Co';
$dat = file_get_contents($url);
$hed = substr($dat, 0, 32);
hexdump($hed);

// TRY TO CREATE AN IMAGE RESOURCE FROM THE IMAGE FILE
$img = ImageCreateFromJPEG($url);
if ($img)
{
    // TRY TO WRITE THE IMAGE FILE
    $out = 'temp_tjome.png';
    if (imagePNG($img, $out))
    {
        echo '<a target="_blank" href="' . $out . '">' . $out . '</a>' . PHP_EOL;
    }
    else
    {
        trigger_error("FAIL: UNABLE TO WRITE $out", E_USER_ERROR);
    }
}
else
{
    trigger_error("FAIL: $url IS NOT A VALID JPEG FILE", E_USER_ERROR);
}

Open in new window

HTH, ~Ray
0
 
tjomeAuthor Commented:
thank you Ray for your help
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using E-E.   It's a great question! ~Ray
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now