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程序设计有所帮助。

相关文章

10个超级有用值得收藏的PHP代码片段

尽管PHP经常被人诟病,被人贬低,被人当玩笑开,事实证明,PHP是全世界网站开发中使用率最高的编程语言。PHP最大的缺点是太简单,语法不严谨,框架体系很弱,但这也是它最大的优点,一个有点...

php记录日志的实现代码

复制代码 代码如下: $ss_log_level = 0;   $ss_log_filename = /tmp/ss-log;   $ss_log_levels = array(   ...

php开发工具之vs2005图解

php开发工具之vs2005图解

为什么会想到用vs2005呢? 一是有感于vs开发.net应用程序,或者asp.net时候的强大。。。。。一直很怀念 二是 vs是个很全面的编辑器,甚至图片都可以直接打开编辑,对于类似我...

浅析PHP程序设计中的MVC编程思想

PHP的MVC编程思想目前已经被广泛使用于各种大型项目的开发,很多成熟的MVC框架也逐渐被大家所熟知并被广泛应用于各类项目中,比较常见的如ThinkPHP、codeigniter、Sym...

PHP基于Closure类创建匿名函数的方法详解

本文实例讲述了PHP基于Closure类创建匿名函数的方法。分享给大家供大家参考,具体如下: Closure 类 用于代表匿名函数的类。 匿名函数(在 PHP 5.3 中被引入)会产生这...