Последния проект изискваше добра галерия с всевъзможни екстри, но най-трудното беше да я накарам да направи миниатюри за анимирани gif-ове без да нарушава анимацията им или качеството. Трудно нали? То е трудно с photoshop, камо ли с php. Ще видим развръзката, ето кода до сега:
<?php
$img = $_GET["img"];
$max = $_GET["max"];
function thumbnail($imgfile,$thumbsize) {
list($width,$height) = getimagesize($imgfile);
$imgratio = $width / $height;
f($imgratio > 1) {
$newwidth = $thumbsize;
$newheight = $thumbsize / $imgratio;
}
else if($imgratio < 1) {
$newheight = $thumbsize;
$newwidth = $thumbsize * $imgratio;
}
else {
$newheight = $thumbsize;
$newwidth = $thumbsize;
}
$thumb = imagecreatetruecolor($newwidth,$newheight);
if (eregi(’.jpg’, $imgfile) || eregi(’.jpеg’, $imgfile)) {
header(’Content-type: image/jpeg’);
$source = imagecreatefromjpeg($imgfile);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, ”, 100);
}
else if (eregi(’.gif’, $imgfile)) {
header(’Content-type: image/gif’);
$source = imagecreatefromgif($imgfile);
$transperent = imagecolortransparent($source);
if ($transparent >= 0) {
imagepalettecopy($source, $thumb);
imagefill($thumb, 0, 0, $transparent);
imagecolortransparent($thumb, $transparent);
imagetruecolortopalette($thumb, true, 256);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagegif($thumb,”,100);
}
else if (eregi(’.png’, $imgfile)) {
header(’Content-type: image/png’);
$source = imagecreatefrompng($imgfile);imagealphablending($thumb, false);
$transperent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
imagefill($thumb, 0, 0, $transparent);
imagesavealpha($thumb, true);
imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);
imagepng($thumb,”,0);
}
imagedestroy($thumb);
}
thumbnail($img, $max);
?>
Доста е омазан, но става.
Използва се така:
<img src="resize.php?img=untitled.jpg&max=150">
RSS
Atom