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

相关文章

php批量删除超链接的实现方法

清除掉一段html文本内容中的超链接最常见的写法可以如下: 复制代码 代码如下:$str=preg_replace("/<a[^>]*href=[^>]*>|&l...

php 三大特点:封装,继承,多态

一.封装 目的:让类更安全 做法:成员变量变为私有的,通过方法间接操作成员变量,在方法里面加限制条件 二.继承 概念:子类可以继承父类的一切 方法重写:在子类里面对父类进行方法重写 特点...

PHP实现数组和对象的相互转换操作示例

本文实例讲述了PHP实现数组和对象的相互转换操作。分享给大家供大家参考,具体如下: 关于php中想让对象以数组的形式访问,这时候就需要使用到get_object_vars()函数了。先来...

php实现获取文章内容第一张图片的方法

本文实例讲述了php实现获取文章内容第一张图片的方法。分享给大家供大家参考。具体分析如下: 采用php获取文章内容的第一张图片方法非常的简单,我们最常用的是使用正则了,感兴趣的朋友可以参...

老版本PHP转义Json里的特殊字符的函数

在给一个 App 做 API,从服务器端的 MySQL 取出数据,然后生成 JSON。数据中有个字段叫 content,里面保存了文章内容,含有大量 HTML 标签,这个字段在转 jso...