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+ajax实现无刷新的新闻留言系统

php+ajax实现无刷新的新闻留言系统

本文介绍了一款无刷新的新闻留言系统,最简明易懂的一个ajax无刷新留言系统,源码中省略了接受数据验证的过程,大家可根据自己的需求进行扩展,下面进入主题。 核心源码: 1.配置文件:co...

PHP Try-catch 语句使用技巧

PHP Try-catch 语句 为了进一步处理异常,我们需要使用try-catch语句----包括Try语句和至少一个的catch语句。任何调用 可能抛出异常的方法的代码都应该使用tr...

php ci框架验证码实例分析

php代码:复制代码 代码如下:<?php class Captcha_code{ var $width='60'; var $num='4'; va...

9个PHP开发常用功能函数小结

1. 函数的任意数目的参数   你可能知道PHP允许你定义一个默认参数的函数。但你可能并不知道PHP还允许你定义一个完全任意的参数的函数   下面是一个示例向你展示了默认参数的函数: 复...

PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法

PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法

本文实例讲述了PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法。分享给大家供大家参考,具体如下: 先来看看前序遍历、中序遍历与后序遍历原理图: 根据树的前序遍历和中序遍历构造...