簡體   English   中英

PHP調整圖像大小然后裁剪圖像問題

[英]php resize image then crop image problem

我想將圖片尺寸從600px * 500px減小到60px * 50px,然后將其裁剪為50px * 50px。 我有兩組代碼,一組是縮小圖像尺寸,另一組是裁切圖像。 問題是它們是分開工作的,如何將這兩組代碼組合在一起使它們一起工作? 以下是我的代碼:

<?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
?>

從上面的兩組代碼中可以看到,第1組調整圖片的大小,然后將其保存到文件夾中。 第二組代碼裁剪圖片,然后也將其保存到文件夾中。 我的問題是...第一組代碼重新調整圖片大小后,是否需要將其保存到文件夾中才能進行裁剪? 如果有必要,那么我需要編寫新的代碼行來從文件夾中檢索已調整大小的圖片,以便第二組代碼進行裁剪? 如果沒有必要,在調整圖片大小之后,如何將圖片傳遞給第二組代碼進行裁剪?

您在這里@ 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);

?>
$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。這將保留原始圖像的完整高度,並從左側切掉50px,從右側切掉50px。 剩余的500px裁剪為50px。

鏈接到我寫的有關如何將任何大小的圖像調整為任意大小的博客文章。 包括用於信箱和裁切的選項。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM