PHP上传和调整图像大小
发布时间:2020-05-26 16:01:56 所属栏目:PHP 来源:互联网
导读:我正在编写一个使用 PHP上传图片的脚本,我想让它在保存之前将图片大小调整为180. 我尝试使用WideImage库和 – saveFileTO(…)但是当我在页面中包含WideImage.php时,页面变为空白!! 所以这是我的脚本,如果你能帮助我并告诉我如何保存调整大小的版本 您可以使用
|
我正在编写一个使用
PHP上传图片的脚本,我想让它在保存之前将图片大小调整为180.
以下代码应该让您了解如何实现调整大小: // Get the image info from the photo
$image_info = getimagesize($photo);
$width = $new_width = $image_info[0];
$height = $new_height = $image_info[1];
$type = $image_info[2];
// Load the image
switch ($type)
{
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($photo);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($photo);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($photo);
break;
default:
die('Error loading '.$photo.' - File type '.$type.' not supported');
}
// Create a new,resized image
$new_width = 180;
$new_height = $height / ($width / $new_width);
$new_image = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_image,$image,$new_width,$new_height,$width,$height);
// Save the new image over the top of the original photo
switch ($type)
{
case IMAGETYPE_JPEG:
imagejpeg($new_image,$photo,100);
break;
case IMAGETYPE_GIF:
imagegif($new_image,$photo);
break;
case IMAGETYPE_PNG:
imagepng($new_image,$photo);
break;
default:
die('Error saving image: '.$photo);
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
