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判断并删除空目录及空子目录的方法

本文实例讲述了php判断并删除空目录及空子目录的方法。分享给大家供大家参考。具体实现方法如下: 步骤如下: 1.遍历目录及子目录 2.使用 scandir 判断目录是否为空,为空则使用r...

利用php操作memcache缓存的基础方法示例

前言 最近在工作中又遇到了memcache,大家应该都有所了解,memcache 是一个高效的分布式的内存对象缓存系统,他可以支持把php的各种数据(数组,对象,基本数据类型)放在它管理...

php安全配置 如何配置使其更安全

另外,目前闹的轰轰烈烈的SQL Injection也是在PHP上有很多利用方式,所以要保证安全,PHP代码编写是一方面,PHP的配置更是非常关键。 我们php手手工安装的,php的默认...

学习php设计模式 php实现门面模式(Facade)

学习php设计模式 php实现门面模式(Facade)

一、意图 为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层次的接口,使得子系统更加容易使用【GOF95】 外部与子系统的通信是通过一个门面(Facade)对象进行。...

php使用json-schema模块实现json校验示例

本文实例讲述了php使用json-schema模块实现json校验。分享给大家供大家参考,具体如下: 客户端和服务端的http信息传递,采用json几乎成了标配。json格式简单,易于处...