PHP 动态调整图像大小和圆角图像

发布于 2024-10-11 03:00:25 字数 2684 浏览 4 评论 0原文

我正在编写一个脚本,该脚本可以动态地将圆角边缘广告到图像上,然后将其裁剪到一定的大小。目前,脚本将圆角边缘广告到图片上,但我无法获取它,因此调整原始图像的大小以适合最终输出图像的尺寸(140px x 120px)。问题是原始上传的图像取决于它的原始图像 中的大小

{   
 $image_file = $_FILES['image']['tmp_name'];

    $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px
    $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == "no") ? false : true; // Top-left rounded corner is shown by default
    $bottomleft = (isset($_GET['bottomleft']) and $_GET['bottomleft'] == "no") ? false : true; // Bottom-left rounded corner is shown by default
    $bottomright = (isset($_GET['bottomright']) and $_GET['bottomright'] == "no") ? false : true; // Bottom-right rounded corner is shown by default
    $topright = (isset($_GET['topright']) and $_GET['topright'] == "no") ? false : true; // Top-right rounded corner is shown by default
    $imagetype=$_FILES['image']['type'];

    $endsize=$corner_radius;
    $startsize=$endsize*3-1;
    $arcsize=$startsize*2+1;

    if (($imagetype=='image/jpeg') or ($imagetype=='jpg')) {
    $image = imagecreatefromjpeg($image_file);
    } else {
    if (($imagetype=='GIF') or ($imagetype=='gif')) {
    $image = imagecreatefromgif($image_file);
    } else {
    $image = imagecreatefrompng($image_file);
    }
    }

    $forecolor ='#ffffff';
    $size = getimagesize($image_file);
    // Top-left corner
    $background = imagecreatetruecolor($size[0],$size[1]);
    imagecopymerge($background, $image, 0, 0, 0, 0, $size[0], $size[1], 100);
    $startx=$size[0]*2-1;
    $starty=$size[1]*2-1;
    $im_temp = imagecreatetruecolor($startx,$starty);
    imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]);
    $bg = imagecolorallocate($im_temp, 255,255,255);
    $fg = imagecolorallocate($im_temp,  255,255,255);

    if ($topleft == true) {
    if(!imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180,270,$bg))echo "nope";
    imagefilltoborder($im_temp,0,0,$bg,$bg);
    }
    // Bottom-left corner

    // Top-right corner
    if ($topright == true) {
    imagearc($im_temp, $startx-$startsize, $startsize,$arcsize, $arcsize, 270,360,$bg);
    imagefilltoborder($im_temp,$startx,0,$bg,$bg);
    }


    $image = imagecreatetruecolor(140,120);
    imagecopyresampled($image, $im_temp, 0, 0, 0, 0, $size[0],$size[1],$starty+1310,$startx+1500);


    // Output final image


    if(!imagepng($image,'hello.png')) echo "boo";
    if(!imagedestroy($image)) echo "2";
    if(!imagedestroy($background)) echo "3";
    if(!imagedestroy($im_temp)) echo "4";

    }

尺寸会改变最终 PNG编辑

:我的问题是如何重新调整原始图像,使其适合使用圆边处理的 140 x 120 图像?

I'm working of a script that dynamically ads rounded edges to an image then crops it down to a certain size. At the moment the script ads the rounded edges to the picture but i cannot get it so the original image is resized to fit within the dimensons of the final outputted image (140px x 120px) The problem is that the orginal uploaded image depending on it's orginal dimensions change the size it is in the final PNG

{   
 $image_file = $_FILES['image']['tmp_name'];

    $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px
    $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == "no") ? false : true; // Top-left rounded corner is shown by default
    $bottomleft = (isset($_GET['bottomleft']) and $_GET['bottomleft'] == "no") ? false : true; // Bottom-left rounded corner is shown by default
    $bottomright = (isset($_GET['bottomright']) and $_GET['bottomright'] == "no") ? false : true; // Bottom-right rounded corner is shown by default
    $topright = (isset($_GET['topright']) and $_GET['topright'] == "no") ? false : true; // Top-right rounded corner is shown by default
    $imagetype=$_FILES['image']['type'];

    $endsize=$corner_radius;
    $startsize=$endsize*3-1;
    $arcsize=$startsize*2+1;

    if (($imagetype=='image/jpeg') or ($imagetype=='jpg')) {
    $image = imagecreatefromjpeg($image_file);
    } else {
    if (($imagetype=='GIF') or ($imagetype=='gif')) {
    $image = imagecreatefromgif($image_file);
    } else {
    $image = imagecreatefrompng($image_file);
    }
    }

    $forecolor ='#ffffff';
    $size = getimagesize($image_file);
    // Top-left corner
    $background = imagecreatetruecolor($size[0],$size[1]);
    imagecopymerge($background, $image, 0, 0, 0, 0, $size[0], $size[1], 100);
    $startx=$size[0]*2-1;
    $starty=$size[1]*2-1;
    $im_temp = imagecreatetruecolor($startx,$starty);
    imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]);
    $bg = imagecolorallocate($im_temp, 255,255,255);
    $fg = imagecolorallocate($im_temp,  255,255,255);

    if ($topleft == true) {
    if(!imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180,270,$bg))echo "nope";
    imagefilltoborder($im_temp,0,0,$bg,$bg);
    }
    // Bottom-left corner

    // Top-right corner
    if ($topright == true) {
    imagearc($im_temp, $startx-$startsize, $startsize,$arcsize, $arcsize, 270,360,$bg);
    imagefilltoborder($im_temp,$startx,0,$bg,$bg);
    }


    $image = imagecreatetruecolor(140,120);
    imagecopyresampled($image, $im_temp, 0, 0, 0, 0, $size[0],$size[1],$starty+1310,$startx+1500);


    // Output final image


    if(!imagepng($image,'hello.png')) echo "boo";
    if(!imagedestroy($image)) echo "2";
    if(!imagedestroy($background)) echo "3";
    if(!imagedestroy($im_temp)) echo "4";

    }

EDIT:

My question is how to get the orginal image reized so it fits into the 140 x 120 image that is processed with the rounded edges?

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

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

发布评论

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

评论(1

百合的盛世恋 2024-10-18 03:00:25

这里是一个 PHP 函数的链接,该函数可以将任何图像调整为任意大小,使用裁剪或信箱来保持纵横比。它有一个非常详尽的解释。但是,您需要在调整大小后添加圆角边缘。

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

here is a link to a PHP function that will resize any image to any arbitrary size, using crop-to-fit or letterboxing to maintain aspect ratio. it has a pretty thorough explaination. you will want to add your rounded edges AFTER the resize however.

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

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