代码之家  ›  专栏  ›  技术社区  ›  chrizonline

PHP裁剪图像以固定宽度和高度而不丢失尺寸比

  •  11
  • chrizonline  · 技术社区  · 15 年前

    我想创建缩略图,有100px的100px尺寸。我已经看过很多文章解释了这些方法,但大多数文章最后都有宽度=保持尺寸比时的高度。

    例如,我有一个450px x 350px的图像。我想裁剪到100px到100px。如果我保持这个比例,我最终会得到100px乘以77px。当我把这些图片列成一行和一列的时候,这让它看起来很难看。但是,没有尺寸比的图像看起来也很糟糕。

    我看过flickr的图片,它们看起来很棒。例如:
    缩略图: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
    http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
    大尺寸: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

    4 回复  |  直到 15 年前
        1
  •  40
  •   Sven Koschnicke    15 年前

    这是通过仅使用图像的一部分作为缩略图来实现的,缩略图的纵横比为1:1(主要是图像的中心)。如果你仔细看,你可以在flickr缩略图中看到它。

    //Your Image
    $imgSrc = "image.jpg";
    
    //getting the image dimensions
    list($width, $height) = getimagesize($imgSrc);
    
    //saving the image into memory (for manipulation with GD Library)
    $myImage = imagecreatefromjpeg($imgSrc);
    
    // calculating the part of the image to use for thumbnail
    if ($width > $height) {
      $y = 0;
      $x = ($width - $height) / 2;
      $smallestSide = $height;
    } else {
      $x = 0;
      $y = ($height - $width) / 2;
      $smallestSide = $width;
    }
    
    // copying the part into thumbnail
    $thumbSize = 100;
    $thumb = imagecreatetruecolor($thumbSize, $thumbSize);
    imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
    
    //final output
    header('Content-type: image/jpeg');
    imagejpeg($thumb);
    
        2
  •  3
  •   ShivarajRH    13 年前

    基于较小宽度或高度裁剪正方形图像

     public function croppThis($target_url) {
    
        $this->jpegImgCrop($target_url);
    
     }
    

    $target\u url-是图像的名称。

     public function jpegImgCrop($target_url) {//support
    
    
    
      $image = imagecreatefromjpeg($target_url);
      $filename = $target_url;
      $width = imagesx($image);
      $height = imagesy($image);
      $image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM
    
      if($width==$height) {
    
       $thumb_width = $width;
       $thumb_height = $height;
    
      } elseif($width<$height) {
    
       $thumb_width = $width;
       $thumb_height = $width;
    
      } elseif($width>$height) {
    
       $thumb_width = $height;
       $thumb_height = $height;
    
      } else {
       $thumb_width = 150;
       $thumb_height = 150;
      }
    
      $original_aspect = $width / $height;
      $thumb_aspect = $thumb_width / $thumb_height;
    
      if ( $original_aspect >= $thumb_aspect ) {
    
         // If image is wider than thumbnail (in aspect ratio sense)
         $new_height = $thumb_height;
         $new_width = $width / ($height / $thumb_height);
    
      }
      else {
         // If the thumbnail is wider than the image
         $new_width = $thumb_width;
         $new_height = $height / ($width / $thumb_width);
      }
    
      $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
    
      // Resize and crop
      imagecopyresampled($thumb,
             $image,
             0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
             0 - ($new_height - $thumb_height) / 2, // Center the image vertically
             0, 0,
             $new_width, $new_height,
             $width, $height);
      imagejpeg($thumb, $filename, 80);
    
     }
    
        3
  •  3
  •   sajad abbasi    8 年前

    你可以使用这个代码。 您需要传递源图像路径和像素大小的缩略图,以及可选的目标路径。如果你通过它将保存图像,否则拇指将显示。

    只允许jpg、jpeg和png。

    function cropImage($sourcePath, $thumbSize, $destination = null) {
    
      $parts = explode('.', $sourcePath);
      $ext = $parts[count($parts) - 1];
      if ($ext == 'jpg' || $ext == 'jpeg') {
        $format = 'jpg';
      } else {
        $format = 'png';
      }
    
      if ($format == 'jpg') {
        $sourceImage = imagecreatefromjpeg($sourcePath);
      }
      if ($format == 'png') {
        $sourceImage = imagecreatefrompng($sourcePath);
      }
    
      list($srcWidth, $srcHeight) = getimagesize($sourcePath);
    
      // calculating the part of the image to use for thumbnail
      if ($srcWidth > $srcHeight) {
        $y = 0;
        $x = ($srcWidth - $srcHeight) / 2;
        $smallestSide = $srcHeight;
      } else {
        $x = 0;
        $y = ($srcHeight - $srcWidth) / 2;
        $smallestSide = $srcWidth;
      }
    
      $destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
      imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
    
      if ($destination == null) {
        header('Content-Type: image/jpeg');
        if ($format == 'jpg') {
          imagejpeg($destinationImage, null, 100);
        }
        if ($format == 'png') {
          imagejpeg($destinationImage);
        }
        if ($destination = null) {
        }
      } else {
        if ($format == 'jpg') {
          imagejpeg($destinationImage, $destination, 100);
        }
        if ($format == 'png') {
          imagepng($destinationImage, $destination);
        }
      }
    }
    
        4
  •  0
  •   David Yell    15 年前

    我喜欢使用GDLib来摆弄图像,它也非常容易处理。那里有很多博客文章和教程。不过,我建议使用一个类或类似的类,因为处理图像中的所有变化可能非常耗时。

        5
  •  0
  •   Albuquerque Web Design    7 年前

    要完成@SvenKoschnicke代码,这里有一个处理其他图像格式的小工具:

    $sourceProperties = getimagesize($imgSrc);
    
     $width = $sourceProperties[0];
    
     $height = $sourceProperties[1];
    
     switch ($sourceProperties[2]) {
    
     case IMAGETYPE_PNG:
            $myImage = imagecreatefrompng($imgSrc); 
            break;
    
      case IMAGETYPE_GIF:
            $myImage = imagecreatefromgif($imgSrc); 
            break;
    
      case IMAGETYPE_JPEG:
            $myImage = imagecreatefromjpeg($imgSrc); 
            break;
     }