计算resize的图像大小比例

我有一个定义的固定宽度和高度来调整图像的大小。 但是,我有这个问题,因为图像可以有任何一种尺寸比例(它可以是垂直水平 )。 在这种情况下,固定的宽度和高度会导致问题。 我想以更聪明的方式计算宽度和高度

例如让我说我已经定义宽度1024px和高度768px 。 我想调整垂直图像(高度1100px,宽度200px) 。 所以在我的情况下,它将调整到固定大小(1024×768) ,所以宽度将从100px增加到768px ,这将是非常丑陋的。 同样,如果图片的高度小于768px ,则会将高度增加到768px

因此,我想根据原始图像大小比例计算新的图像大小。 比方说,如果上面的示例图像应该调整到最大高度768px ,但那么宽度呢? 它已经小于我的“最大宽度”,这是200px ,那么宽度应该保持不变? 还是应该进一步减less?

同样,如果图片的高度为200px,宽度为1100px 。 所以宽度应该减less到1024px ,但高度呢?

第三个问题是,假设高度和宽度都大于最大高度和最大宽度,比如宽度:1100px,高度:4000px 。 现在既然宽度和高度都大于最大宽度和最大高度,但是图像是垂直的,就会使其水平。 那么如何在这种情况下检查是否应根据最大高度或根据最大宽度来调整图像?

我感谢任何帮助。

这里是我个人的图像resize代码的抓包代码。 首先,你需要的数据:

list($originalWidth, $originalHeight) = getimagesize($imageFile); $ratio = $originalWidth / $originalHeight; 

然后,该algorithm尽可能地将图像适合目标大小,保持原始高宽比,而不是将图像拉伸到比原始大的图像:

 $targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight)); if ($ratio < 1) { $targetWidth = $targetHeight * $ratio; } else { $targetHeight = $targetWidth / $ratio; } $srcWidth = $originalWidth; $srcHeight = $originalHeight; $srcX = $srcY = 0; 

这种图像完全填充目标大小,而不是拉伸:

 $targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size); if ($ratio < 1) { $srcX = 0; $srcY = ($originalHeight / 2) - ($originalWidth / 2); $srcWidth = $srcHeight = $originalWidth; } else { $srcY = 0; $srcX = ($originalWidth / 2) - ($originalHeight / 2); $srcWidth = $srcHeight = $originalHeight; } 

这实际上resize:

 $targetImage = imagecreatetruecolor($targetWidth, $targetHeight); imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight); 

在这种情况下, $size只是宽度和高度(平方目标大小)的一个数字。 我相信你可以修改它来使用非平方目标。 它也应该给你一个灵感,你可以使用其他resize的algorithm。

 $ratio = $originalWidth / $originalHeight 

如果你想改变身高:

 $targetWidth = $targetHeight * $ratio 

如果你想改变宽度:

 $targetHeight = $targetWidth / $ratio 

你想要的是保持原始图像的宽高比。 这是图像的宽度和高度之间的比率。 所以你计算你必须在垂直和水平方向调整图像的大小,然后保持两者中较高的一个。 在伪代码中:

 target_height = 768 target_width = 1024 # v_fact and h_fact are the factor by which the original vertical / horizontal # image sizes should be multiplied to get the image to your target size. v_fact = target_height / im_height h_fact = target_width / im_width # you want to resize the image by the same factor in both vertical # and horizontal direction, so you need to pick the correct factor from # v_fact / h_fact so that the largest (relative to target) of the new height/width # equals the target height/width and the smallest is lower than the target. # this is the lowest of the two factors im_fact = min(v_fact, h_fact) new_height = im_height * im_fact new_width = im_width * im_fact image.resize(new_width, new_height) 

这是工作。

 function calculateDimensions($width,$height,$maxwidth,$maxheight) { if($width != $height) { if($width > $height) { $t_width = $maxwidth; $t_height = (($t_width * $height)/$width); //fix height if($t_height > $maxheight) { $t_height = $maxheight; $t_width = (($width * $t_height)/$height); } } else { $t_height = $maxheight; $t_width = (($width * $t_height)/$height); //fix width if($t_width > $maxwidth) { $t_width = $maxwidth; $t_height = (($t_width * $height)/$width); } } } else $t_width = $t_height = min($maxheight,$maxwidth); return array('height'=>(int)$t_height,'width'=>(int)$t_width); } 

检查下面的php代码:

 $new_width = 1024; $new_height = 768; $this_image = "images/my_image"; list($width, $height, $type, $attr) = getimagesize("$this_image"); if ($width > $height) { $image_height = floor(($height/$width)*$new_width); $image_width = $new_width; } else { $image_width = floor(($width/$height)*$new_height); $image_height = $new_height; } echo "<img src='$this_image' height='$image_height' width='$image_width'>"; 

你应该调整它的大小,这取决于哪个属性距离最大值更远。 然后,计算比率。

 if(($w - $w_max) > ($h - $h_max)) { $w_new = $w_max; $h_new = (int) ($h * ($w_max / $w)); } else { $h_new = $h_max; $w_new = (int) ($w * ($h_max / $h)); } 

我遇到了这个问题,没有find合适的答案,于是我自己出面回答这个问题。

我从一些基本的逻辑开始,在咨询了一位math上稍微好一点的朋友后,这就是我们想出来的。

 function calculate_dimensions($width,$height,$max){ if($width != $height){ if($width > $height){ $t_height = $max; $t_width = min(($width * $t_height)/$height); } if($height > $width){ $t_width = $max; $t_height = min(($t_width * $height)/$width) } }else{ if($width > $max){ $t_width = $t_height = $max; } } $res = ['height'=>$t_height,'width'=>$t_width] return $res; } 

这段代码是可重复使用的,所以请自行解决。 只是通过它允许的最大的最小尺寸,它会计算最大的一面的尺寸,所以你会得到一个正确的缩放尺寸,然后你可以居中裁剪,导致正确的缩小和裁剪图像的方形图像。 这对于个人资料图片和缩略图很有用。

我的朋友贾斯汀·吉列特 ( Justin Gillett)提出了交叉倍增的好build议。

此示例将缩小图像以适合创build不超过指定限制(1200 x 675)的图像的定义的像素完美宽高比(16:9)。

设置您的图像比例和任何上限:

 const RATIO_W = 16; const RATIO_H = 9; const RATIO_MULIPLIER_UPPER_LIMIT = 75; 

计算新图像的宽度和高度

 list($imageWidth, $imageHeight) = getimagesize($path_to_image); if( ($imageWidth / $imageHeight) === (self::RATIO_W / self::RATIO_H) ){ return; // Find closest ratio multiple to image size if($imageWidth > $imageHeight){ // landscape $ratioMultiple = round($imageHeight / self::RATIO_H, 0, PHP_ROUND_HALF_DOWN); }else{ // portrait $ratioMultiple = round($imageWidth / self::RATIO_W, 0, PHP_ROUND_HALF_DOWN); } $newWidth = $ratioMultiple * self::RATIO_W; $newHeight = $ratioMultiple * self::RATIO_H; if($newWidth > self::RATIO_W * self::RATIO_MULIPLIER_UPPER_LIMIT|| $newHeight > self::RATIO_H * self::RATIO_MULIPLIER_UPPER_LIMIT){ // File is larger than upper limit $ratioMultiple = self::RATIO_MULIPLIER_UPPER_LIMIT; } $this->tweakMultiplier($ratioMultiple, $imageWidth, $imageHeight); $newWidth = $ratioMultiple * self::RATIO_W; $newHeight = $ratioMultiple * self::RATIO_H; 

调整图像大小

 $originalImage = imagecreatefromjpeg( $tempImagePath ); $newImage = imagecreatetruecolor($newWidth, $newHeight); imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, imagecolorallocate($newImage, 255, 255, 255)); imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight); imagejpeg($newImage, $tempImagePath, 100); 

循环通过因子,直到两个尺寸都小于原始图像尺寸

 protected function tweakMultiplier( &$ratioMultiple, $fitInsideWidth, $fitInsideHeight ){ $newWidth = $ratioMultiple * self::RATIO_W; $newHeight = $ratioMultiple * self::RATIO_H; if($newWidth > $fitInsideWidth || $newHeight > $fitInsideHeight){ echo " Tweak "; $ratioMultiple--; $this->tweakMultiplier($ratioMultiple, $fitInsideWidth, $fitInsideHeight); }else{ return; } } 

如果最大高度或宽度给定或不使用@(jilles de wit)逻辑

注意事项:这些应该已经被定义了!

 $mh = given height limit; //optional $mw = given width limit; //optional $height = $nh =[your original height]; $width = $nw =[your original width]; 

代码

 if($mh || $mw){ if(is_numeric($mh)){$h_fact = $mh / $nh;} if(is_numeric($mw)){$v_fact = $mw / $nw;} if(is_numeric($v_fact) && is_numeric($h_fact) ){$im_fact = min($v_fact, $h_fact);}else{$im_fact=is_numeric($v_fact)?$v_fact:$h_fact;} $nw = $nw * $im_fact; $nh = $nh * $im_fact; } 

重采样

 $dst_img = imagecreatetruecolor($nw,$nh); imagecopyresampled ($dst_img, $image, 0, 0, 0, 0, $nw, $nh, $width , $height); 
 class Image_Aspect_Ratio_Resize { var $image_to_resize; var $new_width; var $new_height; var $ratio; var $new_image_name; var $save_folder; function resize() { if (!file_exists($this->image_to_resize)) { exit("File " . $this->image_to_resize . " does not exist."); } $info = GetImageSize($this->image_to_resize); if (empty($info)) { exit("The file " . $this->image_to_resize . " doesn't seem to be an image."); } $width = $info[0]; $height = $info[1]; $mime = $info['mime']; /* Keep Aspect Ratio? */ if ($this->ratio) { $thumb = ($this->new_width < $width && $this->new_height < $height) ? true : false; // Thumbnail $bigger_image = ($this->new_width > $width || $this->new_height > $height) ? true : false; // Bigger Image if ($thumb) { if ($this->new_width >= $this->new_height) { $x = ($width / $this->new_width); $this->new_height = ($height / $x); } else if ($this->new_height >= $this->new_width) { $x = ($height / $this->new_height); $this->new_width = ($width / $x); } } else if ($bigger_image) { if ($this->new_width >= $width) { $x = ($this->new_width / $width); $this->new_height = ($height * $x); } else if ($this->new_height >= $height) { $x = ($this->new_height / $height); $this->new_width = ($width * $x); } } } $type = substr(strrchr($mime, '/'), 1); switch ($type) { case 'jpeg': $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; break; case 'png': $image_create_func = 'ImageCreateFromPNG'; $image_save_func = 'ImagePNG'; $new_image_ext = 'png'; break; case 'bmp': $image_create_func = 'ImageCreateFromBMP'; $image_save_func = 'ImageBMP'; $new_image_ext = 'bmp'; break; case 'gif': $image_create_func = 'ImageCreateFromGIF'; $image_save_func = 'ImageGIF'; $new_image_ext = 'gif'; break; case 'vnd.wap.wbmp': $image_create_func = 'ImageCreateFromWBMP'; $image_save_func = 'ImageWBMP'; $new_image_ext = 'bmp'; break; case 'xbm': $image_create_func = 'ImageCreateFromXBM'; $image_save_func = 'ImageXBM'; $new_image_ext = 'xbm'; break; default: $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; } // New Image $image_c = ImageCreateTrueColor($this->new_width, $this->new_height); $new_image = $image_create_func($this->image_to_resize); ImageCopyResampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height); if ($this->save_folder) { if ($this->new_image_name) { $new_name = $this->new_image_name . '.' . $new_image_ext; } else { $new_name = $this->new_thumb_name(basename($this->image_to_resize)) . '_resized.' . $new_image_ext; } $save_path = $this->save_folder . $new_name; } else { /* Show the image without saving it to a folder */ header("Content-Type: " . $mime); $image_save_func($image_c); $save_path = ''; } $process = $image_save_func($image_c, $save_path); return array('result' => $process, 'new_file_path' => $save_path); }} 

/ *函数调用* /

 $resize_image = new Image_Aspect_Ratio_Resize; $new_width = (int) $_POST['new_width']; $new_height = (int) $_POST['new_height']; $resize_image->new_width = $new_width; $resize_image->new_height = $new_height; $resize_image->image_to_resize = $image; // Full Path to the file $resize_image->ratio = true; // Keep aspect ratio // Name of the new image (optional) - If it's not set a new will be added automatically $resize_image->new_image_name = 'water_lilies_thumbnail'; /* Path where the new image should be saved. If it's not set the script will output the image without saving it */ $resize_image->save_folder = 'thumbs/'; $process = $resize_image->resize(); // Output image 

你需要的是“维持”宽高比。 最初你有一个尺寸(wxh) 500x1000的图像,这个宽高比是0.5 。 假设你正在改变1000768的高度,你的结果宽度将是0.5 * 768 = 384

另一个例子, 1800 x 1200 ,你的高度是200 ,那么你的宽度是300因为300/2001800/1200也是1.5

祝你好运。