2

画像サイズを600px*500pxから60px*50pxに縮小してから、50px*50pxにトリミングしたいと思います。コードには2つのグループがあります。1つは画像のサイズを縮小するためのもので、もう1つは画像をトリミングするためのものです。問題は、それらが別々に機能することです。この2つのコードグループを組み合わせて、一緒に機能させるにはどうすればよいでしょうか。以下は私のコードです:

<?php

//codes of group A - Reduce the size of image from 600px * 500px to 60px * 50px   
    $save2 = "images/users/" . $image_name_2; //This is the new file you saving
    list($width2, $height2) = getimagesize($file) ; 
    $modwidth2 = 50; 
    $diff2 = $width2 / $modwidth2;
    $modheight2 = $height2 / $diff2; 
    $tn2 = imagecreatetruecolor($modwidth2, $modheight2) ; 
    $image2 = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2) ; 
    imagejpeg($tn2, $save2, 100) ; 



//codes of group B - Crop the image from 60px * 50px to 50px * 50px 
    $save3 = "images/users/" . $image_name_3;  
    list($width3, $height3) = getimagesize($file) ; 
    $modwidth3 = 60;  
    $diff3 = $width3 / $modwidth3;
    $modheight3 = $height3 / $diff3; 

    $left = 0; 
    $top = 0;

    $cropwidth = 50; //thumb size
    $cropheight = 50;

    $tn3 = imagecreatetruecolor($cropwidth, $cropheight) ; 
    $image3 = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ; 
    imagejpeg($tn3, $save3, 100) ; //save the cropped image
?>

上記の2つのグループのコードからわかるように、最初のグループは写真のサイズを変更してから、フォルダーに保存します。コードの2番目のグループは、写真をトリミングしてから、フォルダーにも保存します。私の質問は...コードの最初のグループが画像のサイズを変更した後、トリミングする前にフォルダに保存する必要がありますか?必要に応じて、コードの2番目のグループのフォルダーからサイズ変更された写真を取得してトリミングするために、新しいコード行を記述する必要がありますか?必要がない場合は、写真のサイズを変更した後、写真をコードの2番目のグループに渡してトリミングするにはどうすればよいですか?

4

3 に答える 3

5

こちらが@zac1987です。
画像を引き伸ばすことなく、希望のサイズの正方形のサムネイルを生成する完全なPHPコード。このコードは、pngとjpg/jpegの両方の画像拡張子をサポートしています。設定部分を希望の部分に変更するだけです。コードをコピーして貼り付け、Webサーバーでテストできます。

<?php

    // SETTINGS
    $image_name = 'file.jpg';    // Full path and image name with extension
    $thumb_name = 'thumbnail';   // Generated thumbnail name without extension
    $thumb_side = 100;           // Desired thumbnail side size
    // END OF SETTINGS

    $image_extension = explode('.', $image_name); // I assume that images are named only following 'imagename.ext' pattern

    if (preg_match('/jpg|jpeg/', $image_extension[1])) {
        $src_image = imagecreatefromjpeg($image_name);
        $image_extension = 'jpg';
    } else if (preg_match('/png/', $image_extension[1])) {
        $src_image = imagecreatefrompng($image_name);
        $image_extension = 'png';
    }

    $src_width = imageSX($src_image);   // Width of the original image
    $src_height = imageSY($src_image);  // Height of the original image

    $min_side = min($src_width, $src_height);

    /*********** If you need this part uncomment it
    $ratio = $min_side / $thumb_width;
    $new_width = floor($src_width / $ratio);
    $new_height = floor($src_height / $ratio);
    **********************************************/    

    $dst_image = imagecreatetruecolor($thumb_side, $thumb_side);

    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $thumb_side, $thumb_side, $min_side, $min_side);

    switch ($image_extension)
    {
        case 'jpg':
            imagejpeg($dst_image, $thumb_name . '.jpg', 100);
            break;
        case 'png':
            imagepng($dst_image, $thumb_name . '.jpg', 100);
            break;
    }

    imagedestroy($src_image);
    imagedestroy($dst_image);

?>
于 2011-05-10T21:21:21.090 に答える
1
$modwidth3 = 500;  //I resize the picture width to smaller as 60px.
$diff3 = $width3 / $modwidth3;
$modheight3 = $height3 / $diff3;  //I resize the picture height to smaller.

$left = 1; //getting the left and top coordinate
$top = 1;

$cropwidth = 50; //thumb size
$cropheight = 50;

imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ; 

$modwidth3トリミングする画像の幅と$modheight3高さに対応する必要があります。また$top$leftトリミングする領域の左上の座標と一致する必要があります。元の画像が600x500ピクセルで、サイズを変更して50x50にトリミングする場合は、$ topを0に、$ leftを50(px)に設定する必要があります。$modwidthと$modheightの両方を500に設定する必要があります。これにより、元の画像の高さ全体が維持され、左側から50ピクセル、右側から50ピクセルが切り取られます。残りの500pxは50pxにトリミングされます。

于 2011-05-09T20:16:38.340 に答える
1

任意のサイズの画像を任意のサイズにサイズ変更する方法について書いたブログ記事へのリンク。レターボックスとクロップトゥフィットのオプションが含まれています。

http://www.spotlesswebdesign.com/blog.php?id=1

于 2011-05-09T22:09:21.583 に答える