13

100px x 100px の寸法のサムネイルを作成しようとしています。メソッドを説明する多くの記事を見てきましたが、寸法比を維持する場合、ほとんどの場合、幅!=高さになります。

たとえば、450px x 350px の画像があります。100px × 100px にトリミングしたいと思います。この比率を維持すると、100px x 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

tks

4

5 に答える 5

41

これは、アスペクト比が 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);
于 2010-07-15T13:36:31.263 に答える
3

より小さい幅または高さに基づいて正方形で画像をトリミングする

 public function croppThis($target_url) {

    $this->jpegImgCrop($target_url);

 }

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

 }
于 2013-01-21T06:36:31.313 に答える
0

私は GDLib を使って画像をいじるのが好きです。操作も非常に簡単です。そこにはたくさんのブログ投稿とチュートリアルがあります。ただし、画像のすべてのバリエーションを処理するには非常に時間がかかる可能性があるため、クラスを使用することをお勧めします。

于 2010-07-15T13:39:52.003 に答える
0

@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;
 }
于 2019-05-07T12:10:27.957 に答える