PHP 图片上传时的大小调整
在 PHP 中,我们可以在上传时调整图像大小。PHP 有一个内置库 gd
来执行图像操作;我们可以创建函数来调整上传图像的大小。
本教程演示了在 PHP 中调整图像大小的不同方法。
使用 PHP gd
库在上传时调整图像大小
PHP 有一个 gd
库来对图像执行操作。打开你的 php.ini
文件并执行以下操作:
;extension=php_gd2.dll
在 php.ini
中搜索 gd
并删除评论 ;
在 PHP 8 中,它是 gd
,而 PHP 8 之前的版本是 gd2
。
extension=php_gd2.dll
重新启动服务器,一切顺利。
上传图片并调整大小的示例:
test.php
:
<!DOCTYPE html>
<html>
<head>
<title>PHP Resize Image On upload</title>
</head>
<body>
<div>
<form action="action.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadimage" />
<br><br>
<input type="submit" name="upload" value="Upload" />
</form>
</div>
</body>
</html>
action.php
:
<?php
if(isset($_POST["upload"])) {
if(is_array($_FILES)) {
$uploaded_file = $_FILES['uploadimage']['tmp_name'];
$upl_img_properties = getimagesize($uploaded_file);
$file_name_id= rand(10,100);
$new_file_name = "Resized Image_".$file_name_id;
$folder_path = "upload/";
$img_ext = pathinfo($_FILES['uploadimage']['name'], PATHINFO_EXTENSION);
$image_type = $upl_img_properties[2];
switch ($image_type) {
//for PNG Image
case IMAGETYPE_PNG:
$image_type_id = imagecreatefrompng($uploaded_file);
$target_layer = image_resize($image_type_id, $upl_img_properties[0], $upl_img_properties[1]);
imagepng($target_layer, $folder_path. $new_file_name. ".". $img_ext);
break;
//for GIF Image
case IMAGETYPE_GIF:
$image_type_id = imagecreatefromgif($uploaded_file);
$target_layer = image_resize($image_type_id, $upl_img_properties[0], $upl_img_properties[1]);
imagegif($target_layer, $folder_path. $new_file_name.".". $img_ext);
break;
//for JPEG Image
case IMAGETYPE_JPEG:
$image_type_id = imagecreatefromjpeg($uploaded_file);
$target_layer = image_resize($image_type_id, $upl_img_properties[0], $upl_img_properties[1]);
imagejpeg($target_layer, $folder_path. $new_file_name.".". $img_ext);
break;
default:
echo "Please select a 'PNG', 'GIF'or JPEG image";
exit;
break;
}
//for the record move the uploaded file to the resized image directory
move_uploaded_file($uploaded_file, $folder_path. "Original Uploaded Image_".$file_name_id.".". $img_ext);
echo "Image is resized according to given Width and Height";
}
}
function image_resize($image_type_id, $img_width, $img_height) {
$target_width =1000;
$target_height =1000;
$target_layer= imagecreatetruecolor($target_width, $target_height);
imagecopyresampled($target_layer, $image_type_id,0,0,0,0, $target_width, $target_height, $img_width, $img_height);
return $target_layer;
}
?>
我们可以上传一张图片 PNG, GIF, JPEG
并使用上面的代码调整它的大小。
输出:
在 PHP 中使用 verot.net
库调整图像大小
verot.net
为 PHP 图像操作提供了一个库。Verot 库使图片上传操作变得简单,并提供了不同的图片上传方式。
首先,从 Verot class.upload.php
下载库。我们下载了 zip 文件。在根目录中提取 zip 文件。
将有两个文件夹,src
和 test
。src
包含所有源代码,test
包含上传和处理上传图像的代码。
打开测试文件夹并运行 index.html
:
在索引页面上,有很多选项。
如果要调整图像大小,请转到图像本地操作
。输入你的图像名称并按处理
。
输出:
结论
正如我们所看到的,verot
处理了许多不同大小的图像,并将图像保存在 tmp
目录中。
测试文件夹中的 upload.php
正在执行所有这些操作。我们可以将 upload.php
中的代码用于任何特定活动。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。