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中根据IP地址判断城市实现城市切换或跳转代码

获取IP地址复制代码 代码如下:<?phpfunction GetIP() {    if ($_SERVER["HTTP_X_FORWARDED_F...

PHP中HTML标签过滤技巧

在开发文章系统中正常需要用到HTML标签、JS脚本等其他脚本代码的过滤,稍微尝试了下,感觉简单的htmlspecialchars()函数的过滤效果始终不如strip_tags()函数的过...

解析php php_openssl.dll的作用

一.openssl简介数据加密是信息信息传输中的一个重要组成部分.任何信息都以明文方式传输,确实是个很不安全的做法.所以, 需要对数据进行加密.将明文数据转换为密文数据,再进行传输....

PHP超牛逼无限极分类生成树方法

你还在用浪费时间又浪费内存的递归遍历无限极分类吗,看了该篇文章,我觉得你应该换换了。 这是我在OSChina上看到的一段非常精简的PHP无限极分类生成树方法,巧在引用,整理分享了。 复制...

详解PHP 二维数组排序保持键名不变

详解PHP 二维数组排序保持键名不变

 对二维数组指定的键名排序,首先大家想到的是array_multisort函数,关于array_multisort的用法我之前也写了一篇废话不多言,我们看个实例: <...