Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lib_imageTumbnail($srcFileName, $destFileName,
- $thumbWidth/*120 for example*/, $thumbHeight/*80 for example*/, $quality = 70)
- {
- // получаем картинковый обьект
- $srcImage = imagecreatefromjpeg($srcFileName);
- if(!$srcImage) return false;
- $srcWidth = imagesx($srcImage);
- $srcHeight = imagesy($srcImage);
- $srcRatio = $srcWidth / $srcHeight; // коеф фотки
- $destRatio = $thumbWidth / $thumbHeight; // коеф стандартный
- if($srcRatio > $destRatio)
- {
- // фотка больше, чем надо, в ширину. Вычисляем отступ по горизонтали.
- $croppedWidth = round($srcHeight * $destRatio);
- $croppedHeight = $srcHeight;
- $cropLeft = round( ($srcWidth - $croppedWidth) / 2);
- $cropTop = 0;
- }
- else
- {
- // фотка больше, чем надо, в высоту. Вычисляем отступ по вертикали.
- $croppedHeight = round($srcWidth / $destRatio);
- $croppedWidth = $srcWidth;
- $cropTop = round( ($srcHeight - $croppedHeight) / 2);
- $cropLeft = 0;
- }
- $destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
- imagecopyresampled($destImage, $srcImage, 0, 0, $cropLeft, $cropTop,
- $thumbWidth, $thumbHeight, $croppedWidth, $croppedHeight);
- imagejpeg($destImage, $destFileName, $quality);
- imagedestroy($srcImage);
- imagedestroy($destImage);
- return true;
- }
- lib_imageTumbnail("c:\\test\\interface.jpg", "c:\\test\\int_100_100.jpg", 100, 100);
- lib_imageTumbnail("c:\\test\\interface.jpg", "c:\\test\\int_120_100.jpg", 120, 100);
- lib_imageTumbnail("c:\\test\\interface.jpg", "c:\\test\\int_120_110.jpg", 120, 110);
- lib_imageTumbnail("c:\\test\\interface.jpg", "c:\\test\\int_80_120.jpg", 80, 120);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement