簡體   English   中英

在PHP中裁剪圖像

[英]Cropping image in PHP

我想在PHP中裁剪圖像並保存文件。 我知道你應該使用GD庫,但我不知道如何。 有任何想法嗎?

謝謝

您可以使用imagecopy來裁剪圖像的必需部分。 命令如下:

imagecopy  ( 
    resource $dst_im - the image object ,
    resource $src_im - destination image ,
    int $dst_x - x coordinate in the destination image (use 0) , 
    int $dst_y - y coordinate in the destination image (use 0) , 
    int $src_x - x coordinate in the source image you want to crop , 
    int $src_y - y coordinate in the source image you want to crop , 
    int $src_w - crop width ,
    int $src_h - crop height 
)

來自PHP.net的代碼 - 從源圖像中裁剪出80x40像素的圖像

<?php
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);

// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);
?>

此功能將裁剪圖像保持圖像寬高比:)

 function resize_image_crop($image, $width, $height)
     {

        $w = @imagesx($image); //current width

        $h = @imagesy($image); //current height
        if ((!$w) || (!$h)) { $GLOBALS['errors'][] = 'Image couldn\'t be resized because it wasn\'t a valid image.'; return false; }
        if (($w == $width) && ($h == $height)) { return $image; }  //no resizing needed
        $ratio = $width / $w;       //try max width first...
        $new_w = $width;
        $new_h = $h * $ratio;    
        if ($new_h < $height) {  //if that created an image smaller than what we wanted, try the other way
            $ratio = $height / $h;
            $new_h = $height;
            $new_w = $w * $ratio;
        }
        $image2 = imagecreatetruecolor ($new_w, $new_h);
        imagecopyresampled($image2,$image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);    
        if (($new_h != $height) || ($new_w != $width)) {    //check to see if cropping needs to happen
            $image3 = imagecreatetruecolor ($width, $height);
            if ($new_h > $height) { //crop vertically
                $extra = $new_h - $height;
                $x = 0; //source x
                $y = round($extra / 2); //source y
                imagecopyresampled($image3,$image2, 0, 0, $x, $y, $width, $height, $width, $height);
            } else {
                $extra = $new_w - $width;
                $x = round($extra / 2); //source x
                $y = 0; //source y
                imagecopyresampled($image3,$image2, 0, 0, $x, $y, $width, $height, $width, $height);
            }
            imagedestroy($image2);
            return $image3;
        } else {
            return $image2;
        }
    }

要使用GD裁剪圖像,您需要使用GD方法的組合,如果您在PHP的imagecopyresampled方法文檔中查看“示例#1”,它將向您展示如何裁剪和輸出圖像,您只需要添加一些代碼來捕獲並將輸出寫入文件...

http://us2.php.net/manual/en/function.imagecopyresampled.php

還有其他選項,包括Image Magick ,如果安裝在你的服務器上,可以使用PHP的exec方法(或類似方法)直接訪問,或者你可以安裝PHP Imagick擴展,它可以產生更高質量的圖像,在我看來,是使用起來更直觀,更靈活。

最后,我使用了開源PHPThumb類庫,它具有非常簡單的界面,可以根據服務器上的內容使用多個選項,包括ImageMagick和GD。

我在一些項目中使用這個腳本,它很容易使用: http//shiftingpixel.com/2008/03/03/smart-image-resizer/

該腳本需要PHP 5.1.0(自2005-11-24以來已經出來 - 如果尚未在此版本升級的時間)和GD(良好的Web主機很少丟失)。

以下是在HTML中使用它的示例:

<img src="/image.php/coffee-bean.jpg?width=200&amp;height=200&amp;image=/wp-content/uploads/2008/03/coffee-bean.jpg" alt="Coffee Bean" />

我剛剛創建了這個功能,它可以滿足我的需求,創建一個居中和裁剪的縮略圖。 它是簡化的,不需要像webGautam的答案中所示的多個圖像復制調用。

提供圖像路徑,最終寬度和高度,以及可選的圖像質量。 我這樣做是為了創建縮略圖,因此所有圖像都保存為JPG,如果需要,可以編輯它以適應其他圖像類型。 這里的要點是使用imagecopyresampled生成縮略圖的數學和方法。 使用相同的名稱和圖像大小保存圖像。

function resize_crop_image($image_path, $end_width, $end_height, $quality = '') {
 if ($end_width < 1) $end_width = 100;
 if ($end_height < 1) $end_height = 100;
 if ($quality < 1 || $quality > 100) $quality = 60;

 $image = false;
 $dot = strrpos($image_path,'.');
 $file = substr($image_path,0,$dot).'-'.$end_width.'x'.$end_height.'.jpg';
 $ext = substr($image_path,$dot+1);

 if ($ext == 'jpg' || $ext == 'jpeg') $image = @imagecreatefromjpeg($image_path);
 elseif($ext == 'gif') $image = @imagecreatefromgif($image_path);
 elseif($ext == 'png') $image = @imagecreatefrompng($image_path);

 if ($image) {
  $width = imagesx($image);
  $height = imagesy($image);
  $scale = max($end_width/$width, $end_height/$height);
  $new_width = floor($scale*$width);
  $new_height = floor($scale*$height);
  $x = ($new_width != $end_width ? ($width - $end_width) / 2 : 0);
  $y = ($new_height != $end_height ? ($height - $end_height) / 2 : 0);
  $new_image = @imagecreatetruecolor($new_width, $new_height);

  imagecopyresampled($new_image,$image,0,0,$x,$y,$new_width,$new_height,$width - $x,$height - $y);
  imagedestroy($image);
  imagejpeg($new_image,$file,$quality);
  imagedestroy($new_image);

  return $file;
 }
 return false;
}

您可以使用以下方法裁剪圖像,

/*parameters are 
    $image =source image name
    $width = target width
    $height = height of image
    $scale = scale of image*/
    function resizeImage($image,$width,$height,$scale) {
        //generate new image height and width of source image
        $newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        //Create a new true color image
        $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
        //Create a new image from file 
        $source = imagecreatefromjpeg($image);
        //Copy and resize part of an image with resampling
        imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
        //Output image to file
        imagejpeg($newImage,$image,90);
        //set rights on image file
        chmod($image, 0777);
        //return crop image
        return $image;
    }

暫無
暫無

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

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