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

相关文章

redis 队列操作的例子(php)

入队操作 复制代码 代码如下: <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); while(Tr...

用PHP代替JS玩转DOM的思路及示例代码

事情的起源比较简单,我需要把一个导航页的数据整理好写入数据库。一个比较直观的方法是对html文件进行分析,通用的方法是用php的正则表达式来匹配。但是这样做开发和维护都很困难,代码可读性...

PHP单例模式定义与使用实例详解

本文实例讲述了PHP单例模式定义与使用。分享给大家供大家参考,具体如下: 先简单的介绍一下单例模式。单例模式就是在应用程序中保持某一个类实例只存在一个,而且不可以受外部环境的影响而生成这...

php知道与问问的采集插件代码

最近发现知道和问问小偷的版本越来越多了!! 看过一个百度小偷的网站也达到了pr6。收录十万多!! 在经过 荐礼啦 四十天的实践之后 发现百度对这个确实挺友好的。 从网站访问来看 很多也是...

php实现 data url的图片生成与保存 原创

Data URL是在本地直接绘制图片,不是从服务器加载,所以节省了HTTP连接,起到加速网页的作用。 语法: data:image/jpg;   ...