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中PCRE正则解析代码详解

一、前言 前面的博客里,有对字符集的解析。这里就不是字符集的事儿了,在PHP中很多函数的处理默认是unicode中的UTF-8编码格式。那么废话不多说,直接开始正题。 二、PHP函数mb...

PHP怎样用正则抓取页面中的网址

前言 链接也就是超级链接,是从一个元素(文字、图片、视频等)链接到另一个元素(文字、图片、视频等)。网页中的链接一般有三种,一种是绝对URL超链接,也就是一个页面的完整路径;另一种是相对...

php递归列出所有文件和目录的代码

<?php /*我的程序在国外的SREVER上,自己编的程序存放到哪,我很难记清。 所以编了一个简单的目录递归函数,查看我的程序,很方便的。 */ function tree($d...

一个漂亮的php验证码类(分享)

一个漂亮的php验证码类(分享)

直接上代码:复制代码 代码如下://验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCD...

PHP队列原理及基于队列的写文件案例

PHP队列原理及基于队列的写文件案例

本文实例讲述了PHP队列原理及基于队列的写文件案例。分享给大家供大家参考,具体如下: 队列是一种线性表,按照先进先出的原则进行的: 入队: 出队: PHP实现队列:第一个元素作为队头...