Automaticke zmensovanie fotografii za pomoci PHP
<?php
/*
ImageThumb - Vytvori zmensenu fotografiu z povodnej
SourceImage - Poloha (adresar) povodnej z ktoreho chcete vytvorit zmensenu fotografiu
DestImage - Poloha (adresar) kde chcete ulozit zmensenu fotografiu
Width - Pozadovana zmensena sirka fotografie
Height - Pozadovana zmensena vyska fotografie
Type - Typ fotografie v akom chcete zmensenu fotografiu ulozit (jpg,gif...)
*/
function ImageThumb($sourceImage = "../adresar/velkefotky", $destImage = "../adresar/malefotky", $width = "200px", $height = "150px", $type = "png") {
$type = strtolower($type)
$imageSize = getimagesize($sourceImage);
if($imageSize[0] > $imageSize[1]) {
$newWidth = $width;
$newHeight = $imageSize[1] * ($newWidth / $imageSize[0]);
}
else {
$newHeight = $height;
$newWidth = $imageSize[0] * ($newHeight / $imageSize[1]);
}
switch(image_type_to_mime_type($imageSize[2])) {
case "image/jpeg":
$image = imagecreatefromjpeg($sourceImage);
break;
case "image/gif":
$image = imagecreatefromgif($sourceImage);
break;
case "image/png":
$image = imagecreatefrompng($sourceImage);
break;
default:
$t = image_type_to_mime_type($imageSize[2]);
echo "The file type {$t} is not supported, please use either jpeg, gif, or png";
break;
}
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imageSize[0], $imageSize[1]);
switch($type) {
case "jpg":
case "jpeg":
header("Content-type: image/jpeg");
imagejpeg($thumb, $destImage);
break;
case "gif":
header("Content-type: image/gif");
imagegif($thumb, $destImage);
break;
case "png":
header("Content-type: image/png");
imagepng($thumb, $destImage);
break;
default:
"The image type {$type} is not supported, please choose another.";
break;
}
imagedestroy($image);
imagedestroy($thumb);
}
?>
Update: Na poziadavku z diskusie, premenne ktore je treba zmenit podla vasich potrieb su zvyraznene cervenou farbou