Jump to content

Recommended Posts

I've come across a situation where the thumbs generated by GD library is misbehaving. Here's how it functions normally:

 

Original Image:

photo_folder/abc.jpg

 

Thumb by GD:

photo_folder/thumbs/abc-85x66-filled-enlarged.jpg (based on the gd criteria)

 

PROBLEM. I'm finding in cases with certain images that this is what's it's doing:

 

Original Image:

photo_folder/abc.jpg

 

Thumb by GD:

photo_folder/thumbs/abc-85x66-filled-enlarged-0.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-1.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-2.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-3.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-4.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-5.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-6.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-7.jpg

photo_folder/thumbs/abc-85x66-filled-enlarged-8.jpg

 

It's creating 9 thumbs for the same image. Because the thumbs that are displayed does not look for -0.jpg, -1.jpg, those thumbs are showing a bad link.

 

Any ideas on why it's doing that?

Link to comment
https://forums.phpfreaks.com/topic/86968-gd-library/
Share on other sites

Here's the full code:

 

<?
$image_magick_exists=true;
$mp="/usr/local/bin/"; //image magick path
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT']."";
//echo $DOCUMENT_ROOT;
/* ==================== */
$maxw=$_GET['maxw'];
$maxh=$_GET['maxh'];
$back=$GET['b'];
$w=$_GET['w'];
$h=$_GET['h'];
$pic=$_GET['pic'];

$mode=$_GET['mode'];
$type=$_GET['type'];
$quality=$_GET['q']==''?100:$_GET['q'];
if($pic=='')Exit();
$enlarge=$_GET['enlarge']; //yes/no/ - enlarge smaller images
if(!(($enlarge=="yes") or ($enlarge=="no"))) $enlarge="yes";
$fill=$_GET['fill']; //yes/no - fill whole image or not
if(!(($fill=="yes") or ($fill=="no"))) $fill="yes";
$always_create=($_GET['refresh']=="yes");
//***********************************Load and create image*******************************************
//echo $pic;
//$img_info=getimagesize($pic);
while(substr($pic,0,1)=="/"){$pic=substr($pic,1);}
//$pic="../".$pic;
//echo $pic;
if(!file_exists($pic)) $pic="images/cross.gif";
$img_info=IMgetimagesize("\"".$pic."\"");
$img_w=$img_info[0];
$img_h=$img_info[1];
if($img_w==0 and $img_h==0) $image_magick_exists=false;//return original
//echo $pic;
if(!$image_magick_exists)
{

header("Content-Length: ".filesize($pic));
$fh=fopen($pic, "rb");
$s=fread ($fh, filesize ($pic));
fclose($fh);
echo $s;
exit();
}
if($h=="")
{
$h=ceil(($w*$img_h)/$img_w);
}
elseif($w=="")
{
$w=ceil(($img_w*$h)/$img_h);
}
$path_parts = pathinfo($pic);
$dir=$path_parts["dirname"];
$extension=".".$path_parts["extension"];
$fnm=basename($pic, $extension);
//echo $fnm;
//echo "111";
$thumb_dir = $dir.'/thumbs';

if(!is_dir($thumb_dir."/")) {
//echo "making dir ".$DOCUMENT_ROOT.$thumb_dir."/";
if(!mkdir($thumb_dir,0777)) die("Can't make thumbs dir");
}
else
{
//echo "dir exists: ".$DOCUMENT_ROOT.$thumb_dir;
}
$rfn=$thumb_dir."/".$fnm."-".$w."x".$h."-".($fill=="yes"?"filled":"not_filled")."-".($enlarge=="yes"?"enlarged":"not_enlarged").$extension;

if(($enlarge=='no') and ($img_w<$w and $img_h<$h)) 
{
copy ($pic, $rfn);
}
if(file_exists($rfn)and (!$always_create))// and filemtime($pic)==filemtime($rfn))
{
//	if($type=='')$type=$img_info2[2];
	//Header('Content-type: '.image_type_to_mime_type($type));
	header("Content-Length: ".filesize($rfn));
	$fh=fopen($rfn, "rb");
	$s=fread ($fh, filesize ($rfn));
	fclose($fh);
	echo $s;
	exit();
}


if($fill=="yes")
{
$r1=$w/$h;
$r2=$img_w/$img_h;
//echo "r1=$r1, r2=$r2<br>";
if($r1>=$r2)
{
	//echo "by width<br>";
	$new_w=$w;
	$new_h=ceil($new_w*$img_h/$img_w);
	$off_y=0;
	$off_x=ceil(($new_h-$h)/2);
	$g=$w;
}
else
{
	//echo "by height<br>";
	$new_h=$h;
	$g="x".$w;
	$new_w=ceil($img_w*$new_h/$img_h);
	$off_y=ceil(($new_w-$w)/2);
	$off_x=0;
}
//echo "w=$w, h=$h<br>img_w=$img_w, img_h=$img_h<br>new_w=$new_w, new_h=$new_h";
}
else
{
if($img_w>$img_h)
{
	$new_w=$w;
	$new_h=ceil($new_w*$img_h/$img_w);
}
else
{
	$new_h=$h;
	$new_w=ceil($img_w*$new_h/$img_h);
}
$off_x=0;
$off_y=0;
}
//echo $new_w."x".$new_h;
//***********************************Get new size aspects***************************************************


if($b=="") $b="#ffffff";
$s=$mp."convert -resize ".$new_w."x".$new_h."! \"".$pic."\" \"".$rfn."\"";
//echo $s;
exec($s);
$s=$mp."convert -crop ".$w."x".$h."+".$off_y."+".$off_x." ".$rfn." ".$rfn;
exec($s);
$time=time();
//@touch($pic, $time);
//@touch($rfn, $time);

//header("Content-Length: ".filesize($rfn));
//echo $rfn;
$fh=fopen($rfn, "rb");
$s=fread($fh, filesize ($rfn));
fclose($fh);
echo $s;
exit();
function IMgetimagesize($filename)
{
global $mp, $image_magick_exists;
$x=$mp."identify -format \"%wx%hx%m\" ".$filename;
exec($x, $ans);
$ans=join("",$ans);
$ans=explode("x",$ans);
return $ans;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/86968-gd-library/#findComment-444675
Share on other sites

This is how I use the code where I want to display a thumb created by DG:

 

<img border="0" src="gd.php?pic=/photo_folder/abc.jpg&w=85&h=66&enlarge=yes" width="85" height="66" alt="Image Hosting"/>

 

This code will create the thumb using the GD file (that's the full code I posted, which is the GD file):

 

Original Image:

photo_folder/abc.jpg

 

Thumb by GD:

photo_folder/thumbs/abc-85x66-filled-enlarged.jpg

Link to comment
https://forums.phpfreaks.com/topic/86968-gd-library/#findComment-444680
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.