I am trying to make a thumbnail. And the php function works. I’ve made thumbnails with it. However, certain files do not work. I have narrowed down the problem, but am now stuck. Any help would be appreciated.
The images can be found at http://www.pspsigma.com/image_test.php .The one on the left will not work. The one on the right will. Both are .jpg. Both are originally uploaded via a form, but have been altered for stack overflow so that the function works without a form.
The html is a simple script with file input set action to this .php file
the php:
<?php
$the_name= $_FILES['userfile']['name'];
$tmpname = $_FILES['userfile']['tmp_name'];
$size="1000";
$save_dir="images/thumbs/";
$save_name=$the_name;
$maxisheight= "100";
function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight)
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = getimagesize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
//check if imorig is set
if (!isset($imorig)) {
echo " imorig is not set, ";
}
//I tried setting $x and $y from the width and height of the original file. Still didn't work.
//although the variables were set.
//this makes me think the problem is with the imagecreatefromjpeg function.
list($width, $height, $type, $attr) = getimagesize($tmpname);
echo "getimagesize width= " . $width . ", ";
echo "getimagesize height= " . $height . ", ";
//possible solution, didn't work
//$x = $width;
//$y = $height;
//This is the problem
$x = imagesx($imorig);
$y = imagesy($imorig);
//check that x and y are set
if (!isset($x) && !isset($y)) {
echo" x and y are not set,";
}
elseif (!isset($x)) {
echo" x is not set";
}
elseif (!isset($y)) {
echo" y is not set";
}
else {
echo "|x= " . $x . " |";
echo "|y= " . $y . " |";
}
$woh = (!$maxisheight)? $gis[0] : $gis[1] ;
if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight){
$aw = $size;
$ah = $size * $y / $x;
} else {
$aw = $size * $x / $y;
$ah = $size;
}
}
$im = imagecreatetruecolor($aw,$ah);
if (!$im) {
echo " failure: no im variable. ";
}
else {
if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y)) {
if (!imagejpeg($im, $save_dir.$save_name)) {
echo "failure";}
else {
echo "success";}
}
else {
echo" didn't copy resampled";
}
}
}
img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight);
?>
If I choose the rush vader.jpg file and put it through this function. it echo’s that x and y are set, displays their values ,and success.
If I choose the calm.jpg it will echo “failure: no im variable.” and the $x and $y variables will not have values. If I assign the x and y variables the values from getimagesize($tmp_name) it echoes “didn’t copy resampled”.
Is this a problem with the actual image? or is there some way I can change the code so that every image works?
THANK YOU!!!
UPDATE
I echo’d every variable to see if something was off. The problem appears to be in the type. The type is output as 6 which from the php manual comments i found out is bmp.
I don’t understand how that could be if the extension is .jpg… If this is the case. Is a way to change the type to make it either .jpg, .png, or .gif?
The type from the image calm.jpg is 6 which is a bmp. Therefore the imagecreatefrom… will not work and $imorig is NULL. By adding a function from the php manual called imagecreatefrombmp with label 6. The function works perfecty.
Add to the file:
add:
after
Tested. and everything works. Thanks all!