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

相关文章

如何在Ubuntu下启动Apache的Rewrite功能

在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块。 另外,也可以通过将 /etc/apache2/mods-available/re...

如何提高PHP速度第1/3页

简单的数据缓存技术    近来做了一阵子程序性能的优化工作,有个比较有意思的想法,想提出来和大家交流一下。    Cache是“以空间换时间”策略的典型应用模式,是提...

php+Ajax无刷新验证用户名操作实例详解

php+Ajax无刷新验证用户名操作实例详解

本文实例讲述了php+Ajax无刷新验证用户名操作。分享给大家供大家参考,具体如下: AJAX 简介 AJAX = Asynchronous JavaScript And XML(异步...

在同一窗体中使用PHP来处理多个提交任务

为什么使用多个提交任务? 在我回答这个问题之前,先让我回答一个很显然的问题:既然许多窗体更适合单一的提交按钮,为什么有时人们需要的是两个(或者是更多)的提交按钮? 对这一问题最好的解释方...

PHP 抽象方法与抽象类abstract关键字介绍及应用

PHP 抽象方法与抽象类 abstract 关键字 abstract 关键字用于定义抽象方法与抽象类。 抽象方法 抽象方法指没有方法体的方法,具体就是在方法声明的时候没有 {} 括弧以...