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

tjomeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.