千家信息网

如何使用PHP图片等比缩放类SimpleImage

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍"如何使用PHP图片等比缩放类SimpleImage",在日常操作中,相信很多人在如何使用PHP图片等比缩放类SimpleImage问题上存在疑惑,小编查阅了各式资料,整理出简单好用的
千家信息网最后更新 2026年11月01日如何使用PHP图片等比缩放类SimpleImage

这篇文章主要介绍"如何使用PHP图片等比缩放类SimpleImage",在日常操作中,相信很多人在如何使用PHP图片等比缩放类SimpleImage问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"如何使用PHP图片等比缩放类SimpleImage"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

使用方法示例:
设定宽度,等比例缩放

复制代码 代码如下:

include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth(250);
$image->save('picture2.jpg');?>


设定高度,等比例缩放

复制代码 代码如下:

include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToHeight(500);
$image->save('picture2.jpg');
$image->resizeToHeight(200);
$image->save('picture3.jpg');?>


按比例,缩放至50%

复制代码 代码如下:


include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->scale(50);
$image->save('picture2.jpg');?>


缩放后直接输出到屏幕

复制代码 代码如下:

header('Content-Type: image/jpeg');
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth(150);
$image->output();?>


使用例子:

复制代码 代码如下:

include("SimpleImage.php");//图片处理类在下面

$url="https://cache.yisu.com/upload/information/20201209/266/42940.jpg";
$picfile = down($url);//下载图片(下载图片的路径可以处理完成后清空,这里未进行处理)
$res = new SimpleImage();//图片处理实例
$res = $res->load($picfile);
$tmpfile = tempfile().'.jpg';//创建一个路径文件用来保存图片
$width = '30';//设定图片的宽度
$res->resizeToWidth($width);
$res->save($tmpfile);//把处理后的图片保存(无.jpg后缀)
//这里总共产生了3个文件,一个是下载的图片文件,一个是临时文件,最后一个是处理的图片文件。需要优化清理掉前两个文件。


function down($url)
{
$http = array();
$header = "http://f3.v.veimg.cn";
if ($header) {
$http['header'] = $header;
}

$http['timeout'] = 50;

$ctx = stream_context_create(array(
'http' => $http,
));
$content = @file_get_contents($url, 0, $ctx);
sleep(1);

if (!$content) {
return false;
}

$tmpfile = tempfile();

file_put_contents($tmpfile, $content);

return $tmpfile;
}

function tempfile()
{
$path = dirname(__FILE__);
$path .= '/spider/' . date('Ymd') . '/'.date('His').'-' . (int)(time() / 300);

if (!file_exists($path)) {
mkdir($path, 0777, true);
}

do {
$file = $path . '/' . dechex(mt_rand());
}
while (file_exists($file));

touch($file);

return $file;
}


图片处理类源码(官网地址:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):

复制代码 代码如下:


/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

var $image;
var $image_type;

function load($filename) {

$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {

$this->image = @imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {

$this->image = @imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {

$this->image = @imagecreatefrompng($filename);
}

if (!$this->image) {
return false;
}

return $this;
}

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image,$filename);
}
if( $permissions != null) {

chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image);
}
}
function getWidth() {

return imagesx($this->image);
}
function getHeight() {

return imagesy($this->image);
}
function resizeToHeight($height) {

$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}

function resizeToWidth($width) {
if ($this->getWidth() < $width) {
$width = $this->getWidth();
}
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}

function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}

function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}


function resize2($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {
$current_transparent = imagecolortransparent($this->image);
if($current_transparent != -1) {
$transparent_color = imagecolorsforindex($this->image, $current_transparent);
$current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($new_image, 0, 0, $current_transparent);
imagecolortransparent($new_image, $current_transparent);
} elseif( $this->image_type == IMAGETYPE_PNG) {
imagealphablending($new_image, false);
$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
imagefill($new_image, 0, 0, $color);
imagesavealpha($new_image, true);
}
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}

}

到此,关于"如何使用PHP图片等比缩放类SimpleImage"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

图片 代码 处理 文件 学习 宽度 方法 更多 路径 帮助 实用 接下来 两个 使用方法 例子 后缀 地址 实例 屏幕 文章 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 云南咖啡 上海睿民互联网科技有限公司 亿人帮互联网科技有限公司 美国互联网保险保险科技 互联网金融监管科技数据 金诺互联网科技 上海互联网科技有限公司 汉立互联网科技有限公司联系电话 互联网科技公司的市场部门 深圳前海聚择互联网科技有限公司 天速互联网络科技有限公司 昊天互联网科技有限公司 苏州五指互联网络科技有限 电子科技大学互联网创业大赛 工业互联网展湾科技股份有限公司 浙南科技城互联网大厦招聘 易企秀互联网科技是做什么的 互联网科技创新创业项目 瑞安市蟠桃会互联网科技有限公司 武汉亿视互联网科技有限公司 中国互联网科技工作委员会毛军 国智互联网科技 美国互联网保险保险科技 互联网科技公司怎么样 互联网经济与金融科技 深圳市罗太互联网科技有限公司 中天科技中国工业互联网 亚光科技低轨卫星互联网 保山浩联互联网科技有限公司 每日互联网科技新闻
0