php修改上传图片尺寸的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php修改上传图片尺寸的方法。分享给大家供大家参考。具体实现方法如下:

<?php
// 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,0,0,0,0,$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.
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP中判断变量为空的几种方法分享

1. isset功能:判断变量是否被初始化  说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过  注意:当使用isset来判断数组元素是否被初始...

php打开远程文件的方法和风险及解决方法

PHP有一个配置选项叫allow_url_fopen,该选项默认是有效的。它允许你指向许多类型的资源,并像本地文件一样处理。例如,通过读取URL你可以取得某一个页面的内容(HTML),看...

jQuery+PHP+ajax实现微博加载更多内容列表功能

在一些微博网站上我们经常可以看到这样的应用,微博内容列表上并没有使用分页条,而是一次加载一定数量的记录显示在列表页,当用户浏览到列表页底部时,可以通过单击“查看更多”来加载更多记录。本文...

PHP 万年历实现代码

PHP 万年历实现代码

使用PHP实现万年历功能的要点: •得到当前要处理的月份总共有多少天$days •得到当前要处理的月份的一号是星期几$dayofweek $days的作用:知道要...

获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)

获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)

THINKphp里面有调试运行状态的效果: Process:0.2463s (Load:0.0003s Init:0.0010s Exec:0.1095s Template:0.1355...