Home >Backend Development >PHP Tutorial >PHP image compression

PHP image compression

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-08 09:19:321259browse

Image compression is image cropping. The production process is very similar to image watermarking. The difference is that image compression requires existing images to be copied to memory at a certain ratio.

The code is given below:

<?php

/*打开图片*/
$src = "bg.jpg";
$info = getimagesize($src);
$type = image_type_to_extension($info[2],false);
$fun = "imagecreatefrom".$type;
$image = $fun($src);

/*操作图片*/
//1.内存中建立一个300,200真色彩图片
$image_thumb = imagecreatetruecolor(300,200);
//2.核心步骤,将原图复制到真色彩图片上
imagecopyresampled($image_thumb, $image, 0, 0, 0, 0, 300, 200, $info[0], $info[1]);
//3.销毁原始图片
imagedestroy($image);

/*输出图片*/
//浏览器
header("Content-type:".$info['mime']);
$fun = "image".$type;
$fun($image_thumb);
//保存图片
$fun($image_thumb,'bg_tb.'.$type);
/*销毁图片*/
imagedestroy($image_thumb);

For more information about PHP image operations, you can refer to the following articles:

Image watermark production

Text watermark

Picture verification code

Chinese character verification code

Character verification code

Chinese filter

Introduction to GD library

Copyright Statement : This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced PHP image compression, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Nginx timerNext article:Nginx timer