Forum Moderators: coopster
I'm currently using a PHP library called "phpthumb" which grabs images from a URL, creates thumbnails, and caches them locally for fast and permanent retrieval.
The problem is I've noticed it really isn't so fast. Indeed it's really quite slow.
Does anyone know of alternatives? Features I need are:
- user provides URL of a hosted image
- user also able to upload their own image (JPG, PNG, GIF)
- generate thumbnails with contrained proportions (in a set of standard sizes)
- copy of image cached locally on my server in case the hosted image goes away
- good error handling is important
- detect corrupt or unreadable images and replace with a default JPG
Any suggestions? I'd rather not have to build my own from scratch (though I will if I must)
to make this thread useful, I'll donate one of the functions that made it possible:
this one creates all the dimensions you need to fit an image inside he boundaries of a constrained thumbnail size. It returns an array of values you use
function fitinside($thumbsize,$srcw,$srch){
$proportion = $srcw/$srch;
$dstx = 0;
$dsty = 0;
$srcx = 0;
$srcy = 0;
$dstw = 0;
$dsth = 0;if($srcw >= $srch){
// landscape or square
$dstx = 0;
$dsty = floor(($thumbsize/2) - (($thumbsize / $proportion)/2));
$dstw = $thumbsize;
$dsth = floor($thumbsize / $proportion);
}elseif($srcw < $srch){
//portrait
$dstx = floor(($thumbsize/2) - (($thumbsize * $proportion) / 2));
$dsty = 0;
$dstw = floor($thumbsize * $proportion);
$dsth = $thumbsize;
}return array(
'dstx'=>$dstx,
'dsty'=>$dsty,
'srcx'=>$srcx,
'srcy'=>$srcy,
'dstw'=>$dstw,
'dsth'=>$dsth,
'srcw'=>$srcw,
'srch'=>$srch
);
}
you use it like this, passing the results to imagecopyresampled:
$srcw = [b]imagesx[/b]($originalimage);
$srch = [b]imagesy[/b]($originalimage);$thumbsize = 40;
$thumbimage = [b]imagecreatetruecolor[/b]($thumbsize, $thumbsize);
$background = [b]imagecolorallocate[/b]($thumbimage, 255, 255, 255);
[b]imagefill[/b]($thumbimage,0,0,$background);
$f = [b]fitinside[/b]($thumbsize,$srcw,$srch);
[b]imagecopyresampled[/b]($thumbimage, $originalimage, $f['dstx'], $f['dsty'],$f['srcx'],$f['srcy'],$f['dstw'],$f['dsth'],$f['srcw'],$f['srch']);
[b]imagejpeg[/b]($thumbimage,"/images/".$imagename.".jpg",100);
Any suggestions? I'd rather not have to build my own from scratch (though I will if I must)
I wasn't aware of any other solutions therefore the lack of response. FWIW, this resize function is very close to how I handled it too. I custom rolled my own solution quite a few years ago and added it my image manipulation class.