php 图片上传 修改,php 修改上传图片尺寸示例

本文介绍了一种使用PHP调整上传图片尺寸的方法。通过创建临时文件并利用GD库函数,可以按指定宽度等比例缩放图片,并保持原始宽高比不变,避免图片失真。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搜索热词

为大家讲述一下 PHP修改上传图片的尺寸,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。

经测试代码如下:

/**

* 修改上传图片的尺寸

*

* @param

* @arrange (512.笔记) jb51.cc

**/

// This is the temporary file created by PHP

$uploadedfile = $_FILES['uploadfile']['tmp_name'];

// Create an Image from it so we can do the resize

$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image

list($width,$height)=getimagesize($uploadedfile);

// For our purposes,I have resized the image to be

// 600 pixels wide,and maintain the original aspect

// ratio. This prevents the image from being "stretched"

// or "squashed". If you prefer some max width other than

// 600,simply change the $newwidth variable

$newwidth=600;

$newheight=($height/$width)*600;

$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing,copying from the original

// image into the $tmp image

imagecopyresampled($tmp,$src,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the

// resized,uploaded image file to reside in the ./images subdirectory.

$filename = "images/". $_FILES['uploadfile']['name'];

imagejpeg($tmp,$filename,100);

imagedestroy($src);

imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request

// has completed.

/*** 代码来自编程之家 jb51.cc(jb51.cc) ***/

总结

以上是编程之家为你收集整理的php 修改上传图片尺寸示例全部内容,希望文章能够帮你解决php 修改上传图片尺寸示例所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值