16

PHPで画像を切り抜いて、ファイルを保存したいのですが。GDライブラリを使用することになっていることは知っていますが、方法がわかりません。何か案は?

ありがとう

4

6 に答える 6

24

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);
?>
于 2010-01-05T20:43:37.253 に答える
1

GDを使用して画像を切り抜くには、GDメソッドの組み合わせを使用する必要があります。PHPのメソッドのドキュメントの「例1」を見るとimagecopyresampled、画像を切り抜いて出力する方法が示されています。追加するだけです。出力をキャプチャしてファイルに書き込むためのコード...

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

他のオプションもあります。ImageMagickexecは、サーバーにインストールされている場合、PHPの方法(または同様の方法)を使用して直接アクセスできます。または、PHP Imagick拡張機能をインストールして、より高品質の画像を生成できます。もう少し直感的で柔軟に操作できます。

最後に、オープンソースのPHPThumbクラスライブラリを使用しました。これは非常にシンプルなインターフェイスを備えており、ImageMagickやGDなど、サーバーの内容に応じて複数のオプションを使用できます。

于 2010-01-05T20:53:15.687 に答える
0

私はいくつかのプロジェクトでこのスクリプトを使用していますが、非常に使いやすいです: 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" />
于 2010-01-05T21:01:05.497 に答える
0

この関数を作成したばかりで、ニーズに合わせて機能し、中央に配置されトリミングされたサムネイル画像を作成します。これは合理化されており、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;
}
于 2016-12-02T13:38:51.853 に答える
-1

以下の方法を使用して画像をトリミングできます。

/*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;
    }
于 2015-11-27T06:30:08.260 に答える