I had recently the same problem trying to create thumbnails from the images yuploaded by the users.
The webmaster hosting my site did not want to recompile php with jpeg support. Also I did not want to install a full-fledged software such as ImageMagick.
The turnaround was to install the IJG jpeg library as standalone (instead of as a module compiled in php). Then you can use it with a system() call. See script below.
The only significant problem is that you can't really choose the thumbnail size because only 3 scale factore are available.
script:
creates thumbnails for each jpeg image within a folder
$repertoire is the folder name
$largeur : required width (approx.) in pixels
$qualite : quality of the jpeg compression (5 to 95, default 75)
function traitement_miniatures($repertoire,$largeur,$qualite=75) {
global $SCRIPT_FILENAME;
$rep_complet = dirname($SCRIPT_FILENAME)."/".$repertoire;
$repertoire = "./".$repertoire;
if ( (!is_int($largeur)) or ($largeur<=0) )
return array(False,"Width($largeur) must be grater than zero");
if ( (!is_int($qualite)) or ($qualite<5) or ($qualite>95) )
return array(False,"Quality ($qualite) must be between 5 and 95");
test permissions to the folder
$perms = fileperms($repertoire);
$perms = $perms & 07;
if (!(($perms & 01) and ($perms & 02) and ($perms & 04)))
return array(False,"Folder $rep_complet must be with access Read Write List Execute");
$retour = "";
$rep = opendir($repertoire);
$log = fopen($repertoire."/miniatures.log","a"); # audit trail
while($photo = readdir($rep)) {
$ext=strrchr($photo,".");
select files with .jp* extension except existing thumbnails
if ( ($ext) and (strpos($ext,1,2)=="jp") and (!strpos($photo,"mini.jpg")) ) {
if ( ($detail_photo = getimagesize("$repertoire/$photo"))
and ($detail_photo[2]==2) ) { # it is a jpeg format image
$mini_photo = substr($photo,0,strrpos($photo,"."))."mini.jpg"; # name of thumbnail file
# Do not process an image if thumbnail already present
if (!file_exists("$repertoire/$mini_photo")) {
# compute scaling factor
# only valid : 1/2 1/4 1/8 , find closest value
$rapport = $detail_photo[0] / $largeur ; # $detail_photo[0] : width of source image
if ($rapport > 6) $rapport=8;
elseif ($rapport > 3) $rapport=4;
elseif ($rapport > 1.5) $rapport=2;
# don't create a thumbnail if no reduction
if ($rapport >= 2) {
# create a thumbnail with pnm format then compress to jpeg
$empl="/usr/local/bin"; # full path of jpeg library commands
system("$empl/djpeg -scale 1/$rapport -pnm $rep_complet/$photo | $empl/cjpeg -quality $qualite -outfile $rep_complet/$mini_photo",$rc);
if (!$rc) $rc="Ok"; # return code 0
else $rc="Error $rc";
if ($rc=="Ok")
fwrite($log,"on ".date("d/n/y").", scale 1/".$rapport."°, creation of $mini_photo\n");
else
fwrite($log,"on ".date("d/n/y").", $rc when creating $mini_photo\n");
$retour .= "$mini_photo($rc) ";
}
}
}
}
}
fclose($log);
return array(True,$retour);
}