php gd:当我通过 php 裁剪图像时,一些图像被弄脏了

发布于 2024-08-06 10:33:20 字数 976 浏览 2 评论 0原文

这是我正在谈论的网站 http://makeupbyarpi.com/portfolio.php

你会注意到一些图像的宽度被模糊了-明智的。

我使用的代码是这样的:

$width="500";
$height="636";


 $img_src = $_FILES['galleryimg']['tmp_name'];
 $thumb = "../gallery/".rand(0,100000).".jpg";


 //Create image stream 
 $image = imagecreatefromjpeg($img_src);

 //Gather and store the width and height
 list($image_width, $image_height) = getimagesize($img_src);


 //Resample/resize the image 
 $tmp_img = imagecreatetruecolor($width, $height);
 imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);

 //Attempt to save the new thumbnail 
 if(is_writeable(dirname($thumb))){
  imagejpeg($tmp_img, $thumb, 100);

 }

 //Free memory 
 imagedestroy($tmp_img);
 imagedestroy($image);

上传的图像很大,有时是 3000px x 2000px,我用 php 将其裁剪到 500 x 536,一些基于横向的图像被弄脏了。有没有一个公式可以用来仔细裁剪它,以便图像效果良好?

谢谢

here is the website im talking about
http://makeupbyarpi.com/portfolio.php

you'll notice some of the images are smushed width-wise.

the code i used is this:

$width="500";
$height="636";


 $img_src = $_FILES['galleryimg']['tmp_name'];
 $thumb = "../gallery/".rand(0,100000).".jpg";


 //Create image stream 
 $image = imagecreatefromjpeg($img_src);

 //Gather and store the width and height
 list($image_width, $image_height) = getimagesize($img_src);


 //Resample/resize the image 
 $tmp_img = imagecreatetruecolor($width, $height);
 imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);

 //Attempt to save the new thumbnail 
 if(is_writeable(dirname($thumb))){
  imagejpeg($tmp_img, $thumb, 100);

 }

 //Free memory 
 imagedestroy($tmp_img);
 imagedestroy($image);

the images that get uploaded are huge sometimes 3000px by 2000px and i have php crop it down to 500 x 536 and some landscape based images get smushed. is there a formula i can use to crop it carefully so that the image comes out good?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-08-13 10:33:20

如果需要,您可以调整大小并添加 letterbox。您只需调整宽度大小,然后计算新的高度(假设宽高比与原始高度相同),然后如果高度不等于首选高度,您需要绘制一个黑色矩形(覆盖背景),然后将其居中图像。

您也可以制作一个邮筒,但您的做法与上面完全相同,只是宽度变为高度,高度变为宽度。

编辑:实际上,您可以调整最大的尺寸,如果宽度较大,则调整其尺寸,如果高度较大,则调整其尺寸。根据您调整的大小,您的脚本应该是信箱式或邮筒式。

编辑2:

<?php
    // Define image to resize   
    $img_src = $_FILES['galleryimg']['tmp_name'];
    $thumb = "../gallery/" . rand(0,100000) . ".jpg";

    // Define resize width and height
    $width = 500;
    $height = 636;

    // Open image
    $img = imagecreatefromjpeg($img_src);

    // Store image width and height
    list($img_width, $img_height) = getimagesize($img_src);

    // Create the new image
    $new_img = imagecreatetruecolor($width, $height);

    // Calculate stuff and resize image accordingly
    if (($width/$img_width) < ($height/$img_height)) {
        $new_width = $width;
        $new_height = ($width/$img_width) * $img_height;
        $new_x = 0;
        $new_y = ($height - $new_height) / 2;
    } else {
        $new_width = ($height/$img_height) * $img_width;
        $new_height = $height;
        $new_x = ($width - $new_width) / 2;
        $new_y = 0;
    }

    imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height);

    // Save thumbnail
    if (is_writeable(dirname($thumb))) {
        imagejpeg($new_img, $thumb, 100);
    }

    // Free up resources
    imagedestroy($new_img);
    imagedestroy($img);
?>

抱歉,花了一段时间,我在计算部分遇到了一个小错误,我花了大约10分钟才能修复=/这应该可行。

You could resize and add a letterbox if required. You simply need to resize the width and then calculate the new height (assuming width to height ratio is same as original) then if the height is not equal to the preferred height you need to draw a black rectangle (cover background) and then centre the image.

You could also do a pillarbox, but then you do the exact same as above except that width becomes height and height becomes width.

Edit: Actually, you resize the one that is the biggest, if width is bigger, you resize that and if height is bigger then you resize that. And depending on which one you resize, your script should either letterbox or pillarbox.

EDIT 2:

<?php
    // Define image to resize   
    $img_src = $_FILES['galleryimg']['tmp_name'];
    $thumb = "../gallery/" . rand(0,100000) . ".jpg";

    // Define resize width and height
    $width = 500;
    $height = 636;

    // Open image
    $img = imagecreatefromjpeg($img_src);

    // Store image width and height
    list($img_width, $img_height) = getimagesize($img_src);

    // Create the new image
    $new_img = imagecreatetruecolor($width, $height);

    // Calculate stuff and resize image accordingly
    if (($width/$img_width) < ($height/$img_height)) {
        $new_width = $width;
        $new_height = ($width/$img_width) * $img_height;
        $new_x = 0;
        $new_y = ($height - $new_height) / 2;
    } else {
        $new_width = ($height/$img_height) * $img_width;
        $new_height = $height;
        $new_x = ($width - $new_width) / 2;
        $new_y = 0;
    }

    imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height);

    // Save thumbnail
    if (is_writeable(dirname($thumb))) {
        imagejpeg($new_img, $thumb, 100);
    }

    // Free up resources
    imagedestroy($new_img);
    imagedestroy($img);
?>

Sorry it took a while, I ran across a small bug in the calculation part which I was unable to fix for like 10 minutes =/ This should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文