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分割合并两个字符串的函数。分享给大家供大家参考。具体实现方法如下: 这里实现把两个字符串进行分割合并,例如str1=aaaa,str2=bbbb,合并后生成ababa...

浅谈PHP中单引号和双引号到底有啥区别呢?

在PHP中,字符串的定义可以使用英文单引号' ',也可以使用英文双引号" "。 但是必须使用同一种单或双引号来定义字符串,如:'Hello World"和"Hello World'为非法...

PHP yield关键字功能与用法分析

本文实例讲述了PHP yield关键字功能与用法。分享给大家供大家参考,具体如下: yield 关键字是php5.5版本推出的一个特性。生成器函数的核心是yield关键字。它最简单的调用...

php设计模式介绍之编程惯用法第1/3页

php设计模式介绍之编程惯用法第1/3页

在这里总结的许多编程惯用法都是很值得做为单独一个章节的,甚至一本书的。你应该把这章做为PHP模式设计使用惯用法的相关介绍,而且查看一些列出的参考书来进行更深入的学习。 测试你的代码 可能...

PHP fopen 读取带中文URL地址的一点见解

但昨天在读取一张图片的时候出问题了,后来发现是URL里带中文字符。 例如下面这种情况: 复制代码 代码如下: $files = fopen('/zb_users/upload/20200...