Funkcija skirta sukurti sumažintą paveiksliuką (thumbnail)

ProgramavimasPHP

0 • 4902 •

Įkopijuojam funkciją į php bylą ir naudojam:

function createThumbnail($source,$destination,$new_w,$new_h) {
// read file and create image in memory
$src_img=imagecreatefromjpeg($source);
// get image dimensions
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w = $new_w;
$thumb_h = $old_y * ($new_h / $old_x);
}
if ($old_x < $old_y) {
$thumb_w = $old_x * ($new_w / $old_y);
$thumb_h = $new_h;
}
if ($old_x == $old_y) {
$thumb_w = $new_w;
$thumb_h = $new_h;
}
// create new destination image in memory
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
// copy resampled source image to destination
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// save destination image to file
imagejpeg($dst_img, $destination, 75);
// destroy images from memory
imagedestroy($dst_img);
imagedestroy($src_img);
}
?>

Naudojimas:

  createThumbnail ("pic.jpg", "pic_thumb", 100, 100); 
?>

 

Pamoka pateikta

Facebook