1

画像を切り抜くためのスクリプトに取り組んでいます。jpgに取り組んでいます。しかし、さまざまなif句を経由せずに、さまざまなファイルタイプ(bmp、png、gif)に適用する方法はありますか?

//create the crop
// Original image
$filename = 'uploads/'.$image_name;

$endname = 'uploads/thumbnails/'.$image_name;

// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);

// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 0;
$top = 0;

// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = 200;

// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $crop_width, $crop_height);
imagejpeg($canvas, $endname, 100);

に置き換えimcreatefromjpegてみましたimagecreateが、うまくいきませんでした。

4

2 に答える 2

1

imagecreatefromstringファイルタイプを自動的に検出しますが、画像データを文字列として必要としますが、いつでも実行できます

$current_image = imagecreatefromstring(file_get_contents($filename));
于 2012-05-24T18:42:43.460 に答える
0

scr-ソースファイルの場合 dest-結果のファイルの場合

     $size = getimagesize($src); 

        // Detect file format from MIME-information, detected by getimagesize() function
        // And select proper imagecreatefrom()-function.
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
$icfunc = "imagecreatefrom" . $format;
if (!function_exists($icfunc)){
    echo "Function $icfunc doesn't exists!");
}

    //Here is the magic: We call function, which name is value of variable
$isrc = $icfunc($src);

    // Create new img
$idest = imagecreatetruecolor($width_dest, $height_dest);

    // Copy src img to dest img with resizing
imagecopyresampled(
    $idest, $isrc,  // dest & src img ident
    0,0,      // dest img (x,y) top left corner
    0,0,      // src img (x,y) top left corner
    $width_dest, $height_dest,
    $width_src, $height_src
);
于 2012-05-24T18:42:06.847 に答える