首页 / PHP / PHP:满足条件时改变图像质量?
PHP:满足条件时改变图像质量?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP:满足条件时改变图像质量?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1396字,纯文字阅读大概需要2分钟。
内容图文
我有一个PHP脚本,用户可以上传图像.
如果文件大小大于’X’kbytes,我想让脚本降低图像质量(jpeg).
像这样的东西:
if( $_FILES['uploaded_img']['size'] > $file_size_limit ){
// code that lowers the quality of the uploaded image but keeps the image width and height
}
对此最好的方法是什么?
ps:我不想改变图像的宽度和高度.
解决方法:
你当然可以.做这样的事情.
$upload = $_FILES['uploaded_img'];
$uploadPath = 'new/path/for/upload/';
$uploadName = pathinfo($upload['name'], PATHINFO_FILENAME);
$restrainedQuality = 75; //0 = lowest, 100 = highest. ~75 = default
$sizeLimit = 2000;
if($upload['size'] > $sizeLimit) {
//open a stream for the uploaded image
$streamHandle = @fopen($upload['tmp_name'], 'r');
//create a image resource from the contents of the uploaded image
$resource = imagecreatefromstring(stream_get_contents($streamHandle));
if(!$resource)
die('Something wrong with the upload!');
//close our file stream
@fclose($streamHandle);
//move the uploaded file with a lesser quality
imagejpeg($resource, $uploadPath . $uploadName . '.jpg', $restrainedQuality);
//delete the temporary upload
@unlink($upload['tmp_name']);
} else {
//the file size is less than the limit, just move the temp file into its appropriate directory
move_uploaded_file($upload['tmp_name'], $uploadPath . $upload['name']);
}
这将接受PHP GD支持的任何图像格式(假设它已安装在您的服务器上.很可能是).如果图像小于限制,它只会将原始图像上传到您指定的路径.
内容总结
以上是互联网集市为您收集整理的PHP:满足条件时改变图像质量?全部内容,希望文章能够帮你解决PHP:满足条件时改变图像质量?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】