用比例调整图像大小?

发布于 2024-10-21 18:09:24 字数 660 浏览 0 评论 0原文

我正在使用这个功能

function resize($width,$height) {

    $new_image = imagecreatetruecolor($width, $height);

    imagesavealpha($new_image, true);
    $trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
    imagefill($new_image, 0, 0, $trans_colour); 

    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}

我想做的是制作一个方形图像。我想按最小的属性调整大小,而不是压缩较大的数字。我想把边缘切掉。

因此,如果我有一个 213 x 180 的图像,需要将其大小调整为 150 x 150,

我可以在调用此函数之前将图像大小调整为 150 高度。

我不知道该怎么做是获取宽度并砍掉边缘以获得 150 宽度而不变形。

有谁知道该怎么做?

I am using this function

function resize($width,$height) {

    $new_image = imagecreatetruecolor($width, $height);

    imagesavealpha($new_image, true);
    $trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
    imagefill($new_image, 0, 0, $trans_colour); 

    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}

What I want to do is make a square image. I would like to resize by the smallest attribute, then instead of the larger number being squished. I would like the edges chopped off.

So if I have an image which is 213 x 180 which I need resized to 150 x 150

I can resize the image to 150 height before it hits this function.

What I don't know how to do is take the width and chop off the edges to get 150 width without distortion.

Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥︴琐丶欲为 2024-10-28 18:09:24

我想你所说的“砍掉”边缘是指剪掉你的图像,对吧?

要裁剪图像,您可以使用 imagecopyresized

一个小例子:

$imageSrc = //Your source image;
$tempImage = imagecreatetruecolor(150,150);
// CropStartX et cropStartY have to be computed to suit your needs
imagecopyresized($tempImage,$imageSrc,0,0,$cropStartX,$cropStartY,150,150,150,150);
// $tempImage now contain your cropped image.

By "Chop off" the edge i guess you mean making a crop of you're image, right ?

For croping a image you could use imagecopyresized.

A little example :

$imageSrc = //Your source image;
$tempImage = imagecreatetruecolor(150,150);
// CropStartX et cropStartY have to be computed to suit your needs
imagecopyresized($tempImage,$imageSrc,0,0,$cropStartX,$cropStartY,150,150,150,150);
// $tempImage now contain your cropped image.
旧情别恋 2024-10-28 18:09:24

这是从我的一个旧项目复制的,可以满足您的需要:

  static public function resizeCropAndMove($from_path, $to_path, $max_width, $max_height)
  {
    $image_info = getImageSize($from_path); 
    switch ($image_info['mime']) {
      case 'image/jpeg':  $input = imageCreateFromJPEG($from_path);  break;
      default:
        return false;
    }

   $input_width =  imagesx($input);
   $input_height = imagesy($input);

   $output = imageCreateTrueColor($max_width, $max_height);

   if ($input_width <= $input_height) { //portrait
     $lamda = $max_width / $input_width;

     if ($lamda < 1) {
       $temp_width = (int)round($lamda * $input_width);
       $temp_height = (int)round($lamda * $input_height);

       $temp = imagecreatetruecolor($temp_width, $temp_height);
       imageCopyResampled($temp, $input, 0, 0, 0, 0, $temp_width, $temp_height, $input_width, $input_height);

       $top = (int)round(($temp_height - $max_height) / 2);
       $left = 0;
     }      
   } else { //landscape
     $lamda = $max_height / $input_height;

     if ($lamda < 1) {
       $temp_width = (int)round($lamda * $input_width);
       $temp_height = (int)round($lamda * $input_height);

       $temp = imagecreatetruecolor($temp_width, $temp_height);
       imageCopyResampled($temp, $input, 0, 0, 0, 0, $temp_width, $temp_height, $input_width, $input_height);

       $left = (int)round(($temp_width - $max_width) / 2);
       $top = 0;
     }
   }

   if ($lamda < 1)  {
    imageCopyResampled($output, $temp, 0, 0, $left, $top, $max_width, $max_height, $max_width, $max_height);
    imagePNG($output, $to_path);
    imagedestroy($temp);
   } else {
     imagePNG($input, $to_path);
   }

   imageDestroy($input);
   imageDestroy($output);
  }

This is copied from an old project of mine, does what you need:

  static public function resizeCropAndMove($from_path, $to_path, $max_width, $max_height)
  {
    $image_info = getImageSize($from_path); 
    switch ($image_info['mime']) {
      case 'image/jpeg':  $input = imageCreateFromJPEG($from_path);  break;
      default:
        return false;
    }

   $input_width =  imagesx($input);
   $input_height = imagesy($input);

   $output = imageCreateTrueColor($max_width, $max_height);

   if ($input_width <= $input_height) { //portrait
     $lamda = $max_width / $input_width;

     if ($lamda < 1) {
       $temp_width = (int)round($lamda * $input_width);
       $temp_height = (int)round($lamda * $input_height);

       $temp = imagecreatetruecolor($temp_width, $temp_height);
       imageCopyResampled($temp, $input, 0, 0, 0, 0, $temp_width, $temp_height, $input_width, $input_height);

       $top = (int)round(($temp_height - $max_height) / 2);
       $left = 0;
     }      
   } else { //landscape
     $lamda = $max_height / $input_height;

     if ($lamda < 1) {
       $temp_width = (int)round($lamda * $input_width);
       $temp_height = (int)round($lamda * $input_height);

       $temp = imagecreatetruecolor($temp_width, $temp_height);
       imageCopyResampled($temp, $input, 0, 0, 0, 0, $temp_width, $temp_height, $input_width, $input_height);

       $left = (int)round(($temp_width - $max_width) / 2);
       $top = 0;
     }
   }

   if ($lamda < 1)  {
    imageCopyResampled($output, $temp, 0, 0, $left, $top, $max_width, $max_height, $max_width, $max_height);
    imagePNG($output, $to_path);
    imagedestroy($temp);
   } else {
     imagePNG($input, $to_path);
   }

   imageDestroy($input);
   imageDestroy($output);
  }
只有一腔孤勇 2024-10-28 18:09:24
function createCroppedThumb($thumbSourcePath, $thumbSavePath, $thumbDim){

       // Get dimensions of the original image
        $detail             = getimagesize($thumbSourcePath);
        $current_width      = $detail[0];
        $current_height     = $detail[1];
        $imageType          = $detail[2];

        // The x and y coordinates on the original image where we
        // will begin cropping the image
        $left = 0;
        $top  = 0;

        // This will be the final size of the image (e.g. how many pixels
        // left and down we will be going)
        $crop_width     = $thumbDim;
        $crop_height    = $thumbDim;

        // Resample the image
        $canvas = imagecreatetruecolor($crop_width, $crop_height);

        switch($imageType){
            case '1':
            $current_image  = imagecreatefromgif($thumbSourcePath);
            break;

            case '2':
            $current_image  = imagecreatefromjpeg($thumbSourcePath);
            break;

            case '3':
            $current_image  = imagecreatefrompng($thumbSourcePath);
            break;

            default:
            throw new Exception('unknown image type');
            break;      
        }
        imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);

        switch($imageType){
            case '1':
            imagegif($canvas,$thumbSavePath,100);
            break;

            case '2':
            imagejpeg($canvas,$thumbSavePath,100);
            break;

            case '3':
            imagepng($canvas,$thumbSavePath,100);
            break;

            default:
            throw new Exception('unknown image type');
            break;      
        }
}
function createCroppedThumb($thumbSourcePath, $thumbSavePath, $thumbDim){

       // Get dimensions of the original image
        $detail             = getimagesize($thumbSourcePath);
        $current_width      = $detail[0];
        $current_height     = $detail[1];
        $imageType          = $detail[2];

        // The x and y coordinates on the original image where we
        // will begin cropping the image
        $left = 0;
        $top  = 0;

        // This will be the final size of the image (e.g. how many pixels
        // left and down we will be going)
        $crop_width     = $thumbDim;
        $crop_height    = $thumbDim;

        // Resample the image
        $canvas = imagecreatetruecolor($crop_width, $crop_height);

        switch($imageType){
            case '1':
            $current_image  = imagecreatefromgif($thumbSourcePath);
            break;

            case '2':
            $current_image  = imagecreatefromjpeg($thumbSourcePath);
            break;

            case '3':
            $current_image  = imagecreatefrompng($thumbSourcePath);
            break;

            default:
            throw new Exception('unknown image type');
            break;      
        }
        imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);

        switch($imageType){
            case '1':
            imagegif($canvas,$thumbSavePath,100);
            break;

            case '2':
            imagejpeg($canvas,$thumbSavePath,100);
            break;

            case '3':
            imagepng($canvas,$thumbSavePath,100);
            break;

            default:
            throw new Exception('unknown image type');
            break;      
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文