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

相关文章

Thinkphp框架开发移动端接口(1)

本文实例为大家分享了使用Thinkphp框架开发移动端接口代码,给原生APP提供api接口,具体内容如下 1. 使用TP框架时 放在common文件夹下文件名就叫function.php...

php身份证号码检查类实例

本文实例讲述了php身份证号码检查类。分享给大家供大家参考。具体如下: <?php class CIDMaker // 声明一个身份证号码检查类 { var $i...

Yii2设置默认控制器的两种方法

Yii2设置默认控制器的两种方法

本文主要给大家介绍了关于Yii2默认控制器设置的内容,分享了两种方法供大家参考学习,下面来一起看看详细的介绍: 方法1: 首先Yii2中在/vendor/yiisoft/yii2/web...

浅谈PHP接入(第三方登录)QQ登录 OAuth2.0 过程中遇到的坑

前言 绝大多数网站都集成了第三方登录,降低了注册门槛,增强了用户体验。最近看了看 QQ 互联上 QQ 登录的接口文档。接入 QQ 登录的一般流程呢,是这样的:先申请开发者 -> 然...

ajax缓存问题解决途径

我用PHP和Ajax结合,添加数据之后,刷新前台页面,数据没有变化。我改动PHP动态脚本,只有重新找开IE再输入地址,才能看到效果。以上这些是不是缓存的原因啊?怎么解决? ajax缓存问...