[英]PHP: Image Resize and Crop to Portrait
我需要將尺寸和裁剪圖像居中。 最終結果圖像需要為250W x 330H。
我需要調整上傳到330高度的圖像的大小,但保留正確的寬度比例。 然后在重新調整大小后檢查寬度是否為250或更高。 如果不是,那么我需要將圖像從原始尺寸調整為250寬度,但保留與高度的正確比例。
因此,如果它的大小達到330高度且寬度為250或更高,那么我需要將圖像裁剪到寬度為250的中心。但如果它的大小設置為250寬度,高度為330或更高,那么我需要將圖像裁剪到高度為330的中心。
我試圖自己創造它,但我對作物到中心部分感到困惑。
使用Wideimage庫( http://wideimage.sourceforge.net/ ):
$thumb = WideImage::load('uploaded_image.png')->resize(250, 330);
if ($thumb->getWidth() > 250 || $thumb->getHeight() > 330) {
$thumb = $thumb->crop('center', 'center', 250, 330);
}
$thumb->saveToFile('cropped_image.png');
我寫了一個庫就可以做到這一點: Php Image Magician
<?php
require_once('../php_image_magician.php');
$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(250, 330, 'crop');
$magicianObj -> saveImage('racecar_cropped.jpg', 100);
?>
這是一個功能我剛剛完成強制一個精確的像素大小 - 我不能保證它100%但我已經測試了很多選項,並得到了完美的結果到目前為止,它給出了最接近的結果imo。 首先,它通過計算比率來調整源圖像和指定大小之間的最小差異。 然后修剪掉多余的像素。 我已經補償了奇數,負值等等。到目前為止,我已經取得了很好的成績。 如果我錯過了什么或者它是否以某種方式破壞,請告訴我:
PHP:
// set source/export paths and pixel sizes for final sizes
$src="path/to/source.jpg";
$exp="path/to/output.jpg";
$crop_w=300;
$crop_h=200;
$size = getimagesize("$src");
//check image sizes
if( ($size[0] < $crop_w) || ($size[1] < $crop_h) ){
echo 'Image not big enough to crop';
exit();
}
//get differential ratios of image vs crop sizes -
//smaller ratio must be resized
$ratio_w = $size[0]/$crop_w;
$ratio_h = $size[1]/$crop_h;
//square or landscape - shave sides
if($ratio_w >= $ratio_h){
//resize width / shave top&bottom
exec("convert $src -resize x".$crop_h." $exp ");
$size = getimagesize("$exp");
$diff=abs($crop_w-$size[1]);
//dividing 1 by 2 will leave a zero on round down - just force resize
if($diff < 2){
// skip shave - diff too small
exec('convert $exp -resize '.$crop_h.'X! $exp ');
}
else{
//divide difference by 2 for shave amount
$shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller
exec('convert '.$exp.' -shave '.$shave.'x0 '.$exp.' '); //shave sides
//odd $diff leave a rounded down pixel - force height resize
if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
exec('convert '.$exp.' -resize '.$crop_w.'x! '.$exp.' ');
}
}
}
//portrait - shave height
else{
//resize width / shave top&bottom
exec("convert $src -resize ".$crop_w."x $exp ");
$size = getimagesize("$exp");
$diff=abs($crop_h-$size[1]);
//dividing 1 by 2 will leave a zero on round down - just force resize
if($diff < 2){
exec('convert $exp -resize x'.$crop_h.'! $exp ');
}
else{
//divide difference by 2 for shave amount
$shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller
exec('convert '.$exp.' -shave 0x'.$shave.' '.$exp.' '); //shave sides
//odd $diff leave a rounded down pixel - force height resize
if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
exec('convert '.$exp.' -resize x'.$crop_h.'! '.$exp.' ');
}
}
}
隨意使用/發表評論。 Php 5.4 <,Imagemagick 6.8.8.1,Windows xampp。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.