64

以下のコードは画像をうまくトリミングしますが、これは私が望むものですが、より大きな画像の場合も同様に機能します。「画像をズームアウトする」方法はありますか

理想的には、毎回良い結果が得られるように、トリミングする前に各画像をほぼ同じサイズにすることができます

コードは

<?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>';
4

8 に答える 8

126

サムネイルを生成する場合は、最初にを使用して画像のサイズを変更する必要がありますimagecopyresampled();。画像の小さい方のサイズが親指の対応する側と等しくなるように、画像のサイズを変更する必要があります。

たとえば、ソース画像が1280x800pxで、親指が200x150pxの場合、画像のサイズを240x150pxに変更してから、200x150pxにトリミングする必要があります。これは、画像のアスペクト比が変化しないようにするためです。

サムネイルを作成するための一般的な式は次のとおりです。

$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg';

$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);

これはテストしていませんが、動作するはずです。

編集

現在、テストされ、動作しています。

于 2009-12-06T17:55:45.747 に答える
16

imagecopyresampled()$src_image位置で幅と高さの長方形の領域を取り、位置で幅$src_wと高さの$src_h長方形の領域($src_x, $src_y)に配置します。$dst_image$dst_w$dst_h($dst_x, $dst_y)

ソースとデスティネーションの座標と幅と高さが異なる場合は、画像フラグメントの適切な拡大または縮小が実行されます。座標は左上隅を参照しています。

この機能は、同じ画像内の領域をコピーするために使用できます。ただし、リージョンが重複している場合、結果は予測できません。

- 編集 -

$src_wとがそれぞれと$src_hよりも小さい場合は、親指の画像がズームインされます。それ以外の場合は、ズームアウトされます。$dst_w$dst_h

<?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');
?>
于 2012-08-13T10:38:39.307 に答える
3

PHP 5.5 には imagecrop 関数がありますhttp://php.net/manual/en/function.imagecrop.php

于 2016-03-26T14:22:46.747 に答える
0

HTML コード:-

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>

アップロード.php

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;
      ?>  
于 2016-12-21T16:13:05.750 に答える
-1
$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg'

次のものに置き換える必要があります。

$image = imagecreatefromjpeg($_GET['src']);

それならうまくいく!

于 2011-09-19T09:34:16.767 に答える