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防止SQL注入详解及防范

一个是没有对输入的数据进行过滤(过滤输入),还有一个是没有对发送到数据库的数据进行转义(转义输出)。这两个重要的步骤缺一不可,需要同时加以特别关注以减少程序错误。对于攻击者来说,进行SQ...

在WordPress中获取数据库字段内容和添加主题设置菜单

在WordPress中获取数据库字段内容和添加主题设置菜单

get_option() 函数使用技巧 get_option()这个函数,实际上我们在整合后台功能的时候经常会用到的一个函数,主要用来从 WordPress 博客数据库 option 表...

使用WordPress发送电子邮件的相关PHP函数用法解析

使用WordPress发送电子邮件的相关PHP函数用法解析

wp_mail() 函数用来发送邮件,类似于 PHP 的 mail() 函数。 默认的发件人名称是 WordPress,发件人邮箱是类似 wordpress@example.com。 用...

php中抓取网页内容的实例详解

php中抓取网页内容的实例详解 方法一: 使用file_get_contents方法实现 $url = "http://news.sina.com.cn/c/nd/2016-10...

php微信支付接口开发程序

php微信支付接口开发程序讲解: 必要条件: appid //公众号后台开发者中心获得(和邮件内的一样)   mchid//邮件内获得  key//商户后台...