【问题标题】:PHP: Image Resize and Crop to PortraitPHP:图像调整大小和裁剪为纵向
【发布时间】:2013-02-20 06:55:03
【问题描述】:

我需要重新调整大小并将图像裁剪到中心。最终结果图像需要为 250W x 330H。

我需要将上传的图片重新调整为 330 高度,但要保持与宽度的正确比例。然后在重新调整大小后检查宽度是否为 250 或更大。如果不是,那么我需要将图像从原始大小重新调整为 250 宽度,但保持与高度的正确比例。

因此,如果它确实将大小调整为 330 高度并且宽度为 250 或更大,那么我需要将图像裁剪到宽度为 250 的中心。但如果它调整为 250 宽度,高度为 330 或更大,然后我需要将图像裁剪到高度为 330 的中心。

我试图自己创建它,但我被裁剪到中心部分弄糊涂了。

【问题讨论】:

    标签: php image resize crop


    【解决方案1】:

    使用 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');
    

    【讨论】:

      【解决方案2】:

      我写了一个库来做到这一点: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);
      ?>
      

      【讨论】:

        【解决方案3】:

        这是一个我刚刚完成的功能,用于强制精确的像素大小 - 我不能保证它 100%,但我用许多选项对其进行了测试,到目前为止得到了完美的结果,它给出了最接近的结果。首先,它通过计算比率来调整源图像和指定尺寸之间的最小差异。然后修剪掉多余的像素。我已经补偿了奇数,负值等。到目前为止我已经取得了不错的成绩。如果我错过了什么或者它是否以某种方式损坏,请告诉我:

        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.' ');
                    }
                }
        
        
        
            }
        

        随意使用/制作 cmets。 PHP 5.4

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多