[英]PHP image resize and rounded image corners dynamically
我正在使用一個腳本,該腳本可以動態地將圓角的邊緣廣告到圖像上,然后將其裁剪為一定大小。 目前,腳本將圖片的圓角邊緣廣告了,但我無法獲取它,因此原始圖像的大小被調整為適合最終輸出圖像的尺寸(140px x 120px)。問題是原始圖像取決於原始圖像尺寸會更改最終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";
}
編輯:
我的問題是如何調整原始圖像的大小,使其適合使用圓角邊緣處理的140 x 120圖像?
這是一個PHP函數的鏈接,該函數將使用裁剪到適合或letterbox保持縱橫比的方式將任何圖像調整為任意大小。 它有相當詳盡的解釋。 您需要在調整大小后添加圓角邊緣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.