首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP中的裁剪图像

PHP中的裁剪图像
EN

Stack Overflow用户
提问于 2009-12-07 01:39:59
回答 6查看 151.9K关注 0票数 64

下面的代码很好地裁剪图像,这是我想要的,但对于较大的图像,它也可以工作。有没有办法“缩小图像”?

理想情况下,我将能够让每个图像在裁剪之前大致相同的大小,以便我每次都能得到好的结果

代码是

代码语言:javascript
复制
<?php

$image = $_GET['src']; // the image to crop
$dest_image = 'images/cropped_whatever.jpg'; // make sure the directory is writeable

$img = imagecreatetruecolor('200','150');
$org_img = imagecreatefromjpeg($image);
$ims = getimagesize($image);
imagecopy($img,$org_img, 0, 0, 20, 20, 200, 150);
imagejpeg($img,$dest_image,90);
imagedestroy($img);
echo '<img src="'.$dest_image.'" ><p>';
EN

回答 6

Stack Overflow用户

发布于 2012-08-13 18:38:39

imagecopyresampled()将从位置为($src_x, $src_y)的宽度为$src_w和高度为$src_h$src_image中获取一个矩形区域,并将其放置在位置为($dst_x, $dst_y)的宽度为$dst_w和高度为$dst_h$dst_image中的矩形区域中。

如果源和目标坐标以及宽度和高度不同,将对图像片段执行适当的拉伸或收缩。坐标指的是左上角。

此函数可用于复制同一镜像中的区域。但如果区域重叠,结果将是不可预测的。

-编辑-

如果$src_w$src_h分别小于$dst_w$dst_h,则会放大拇指图像。否则,它将被缩小。

代码语言:javascript
复制
<?php
$dst_x = 0;   // X-coordinate of destination point
$dst_y = 0;   // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // Crop end X position in original image
$src_h = 220; // Crop end Y position in original image

// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/source.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
?>
票数 16
EN

Stack Overflow用户

发布于 2016-03-26 22:22:47

票数 3
EN

Stack Overflow用户

发布于 2016-12-22 00:13:06

HTML代码:-

代码语言:javascript
复制
enter code here
  <!DOCTYPE html>
  <html>
  <body>

  <form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="image" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
 </form>

 </body>
 </html>

upload.php

代码语言:javascript
复制
enter code here
<?php 
      $image = $_FILES;
      $NewImageName = rand(4,10000)."-". $image['image']['name'];
      $destination = realpath('../images/testing').'/';
      move_uploaded_file($image['image']['tmp_name'], $destination.$NewImageName);
      $image = imagecreatefromjpeg($destination.$NewImageName);
      $filename = $destination.$NewImageName;

      $thumb_width = 200;
      $thumb_height = 150;

      $width = imagesx($image);
      $height = imagesy($image);

      $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);
      echo "cropped"; die;
      ?>  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1855996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档