php修改上传图片尺寸的方法

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

相关文章

phpmyadmin的#1251问题

mysql> update mysql.user SET Password = OLD_PASSWORD('newpwd...

完美解决PHP中文乱码

一.首先是PHP网页的编码 1. php文件本身的编码与网页的编码应匹配 a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/ht...

php中使用websocket详解

在PHP中,开发者需要考虑的东西比较多,从socket的连接、建立、绑定、监听等都需要开发者自己去操作完成,对于初学者来说,难度方面也挺大的,所以本文的思路如下: 1、socket协议的...

PHP图片处理之使用imagecopyresampled函数裁剪图片例子

图片裁剪是指在一个大的背景图片中裁剪出一张指定区域的图片,常见的应用是在用户设置个人头像时,可以从上传的图片中,裁剪出一个合适的区域作为自己的个人头像图片。图像裁剪和图片缩放的相似,所以...

php多任务程序实例解析

本文以实例简单解析了php多任务程序的实现方法,具体代码如下: <?php error_reporting(E_ALL); set_time_limit(0); /**...