2010-10-01 158 views
1

我想要一个功能,当我上传一张照片时,它应该裁剪图像,而不管中心图像的比例如何,以确保裁切在图像内部。PHP图像调整大小和裁剪功能

alt text

上面的图片是2592×1944

我要裁剪的159 * 129

alt text

的图像,这是使用一个插件,当我得到什么cakephp(Miles Johnsons上传插件)

有人可以帮我找到一个图像裁剪功能来做到这一点或者帮助我完成相同的算法。

+2

您的意思是'resize'而不是'crop'吗? – 2010-10-01 05:07:33

+0

调整大小,裁剪或调整大小和作物? – stillstanding 2010-10-01 05:13:45

+0

我不知道如何以正确的方式做到这一点。我希望最终的图像是图像的最大部分,但我需要的大小。所以我认为它可能会调整大小和作物或其他组合。我不确定。 – 2010-10-01 08:14:03

回答

1

我用这个:http://shiftingpixel.com/2008/03/03/smart-image-resizer/创造一切在这里找到了图片的缩略图:http://www.patriciashin.com/painting.php

+1

对不起,我没有添加更多,我的笔记本电脑电池死亡。无论如何,这个脚本是非常可定制的。他们的文档非常好。我想我甚至可以调整脚本以更好地满足我的需求。 Cakephp可以很容易地将其转换为第三方脚本,但是您可能必须将此脚本演示为最有效的类。让我知道你的想法,我愿意帮忙。 – gregghz 2010-10-01 06:06:49

+0

看起来很有希望。我打算构建一个插件,将其作为开源发布。你会感兴趣吗 ?因为那里的脚本我没有发现他们很多做适当的调整和裁剪。 – 2010-10-01 08:18:41

+0

当然我会很感兴趣。只需与我联系即可进行协调。我并不总是有很多时间,但我会尽我所能帮助。 – gregghz 2010-10-01 14:59:53

1

我必须说,我还没有彻底测试这段代码,我修改了它为个人使用,这将有助于你的问题。

替换插件/上载/供应商的功能作物/ uploader.php

近线368

用下面的函数

public function crop(array $options = array(), $explicit = false) { 
    if ($this->_data[$this->_current]['group'] != 'image' || !$this->_enabled) { 
     return false; 
    } 

    $options = $options + array('location' => self::LOC_CENTER, 'quality' => 100, 'width' => null, 'height' => null, 'append' => null, 'prepend' => null); 
    $width = $this->_data[$this->_current]['width']; 
    $height = $this->_data[$this->_current]['height']; 
    $src_x = 0; 
    $src_y = 0; 
    $dest_w = $width; 
    $dest_h = $height; 
    $location = $options['location']; 

    if (is_numeric($options['width']) && is_numeric($options['height'])) { 
     $newWidth = $options['width']; 
     $newHeight = $options['height']; 

     if ($width/$newWidth > $height/$newHeight) { 
      $dest_h = $options['height']; 
      $dest_w = round($width/($height/$newHeight)); 
     } else { 
      $dest_w = $options['width']; 
      $dest_h = round($height/($width/$newWidth)); 
     } 
    } else { 
     if ($width > $height) { 
      $newWidth = $height; 
      $newHeight = $height; 
     } else { 
      $newWidth = $width; 
      $newHeight = $width; 
     } 

     $dest_h = $newHeight; 
     $dest_w = $newWidth; 
    } 

    $src_x = 0; 
    $src_y = 0; 
    if ($dest_w > $newWidth) { 
      $src_x = ceil((($dest_w - $newWidth)/2) * ($height/$newHeight)); 
    } 

    if ($dest_h > $newHeight) { 
      $src_y = ceil((($dest_h - $newHeight)/2) * ($width/$newWidth)); 
    } 

    $append = '_cropped_' . $newWidth . 'x' . $newHeight; 

    if ($options['append'] !== false && empty($options['append'])) { 
     $options['append'] = $append; 
    } 

    $transform = array(
     'width' => $newWidth, 
     'height' => $newHeight, 
     'source_x' => $src_x, 
     'source_y' => $src_y, 
     'source_w' => $width, 
     'source_h' => $height, 
     'dest_w' => $dest_w, 
     'dest_h' => $dest_h, 
     'target' => $this->setDestination($this->_data[$this->_current]['name'], true, $options, false), 
     'quality' => $options['quality'] 
    ); 
    if ($this->transform($transform)) { 

     return $this->_returnData($transform, $append, $explicit); 
    } 

    return false; 
} 

此致。