簡體   English   中英

PHP圖像調整大小和裁剪功能

[英]PHP image Resize and Crop Function

我想要一個函數,當我上傳照片時,無論從中心到圖像的比例如何,都應裁剪圖像,以確保裁剪良好地位於圖像內部。

替代文字

上面的圖像是2592 * 1944

我想裁剪159 * 129的圖像

替代文字

這就是我使用cakephp插件時得到的結果(Miles Johnsons上傳插件)

可以幫助我找到一個圖像裁剪功能來執行此操作,還是可以幫助我使用該算法來執行此操作?

此問題已解決,請檢出最新版本的Cake-Uploader Pluign。 https://github.com/milesj/cake-uploader/commit/2be63f32730755cffbace17ee8fa2d686785964d

我已經使用了它: http : //shiftingpixel.com/2008/03/03/smart-image-resizer/創建了所有在此處找到的圖像縮略圖: http : //www.patriciashin.com/painting.php

我必須說我還沒有徹底測試過這段代碼,我對其進行了修改以供個人使用,這應該可以解決您的問題。

替換plugin / uploader / vendor / uploader.php中的函數crop

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;
}

親切的問候。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM