How to implement proportional scaling of images in php
Mainly introduces how to use PHP to scale images in equal proportions, and you can specify the required width and length of the original image.
This article introduces the method of image compression in PHP. Readers can refer to the specific application or To improve it, directly enter the code. The function imgzip($src,$newwid,$newhei) brings in the original image, the width required for scaling, and the length of scaling.
<?php //压缩图片 缩略图 $src= "xiezheng.jpg"; $newwid=640; $newhei= 480; function imgzip($src,$newwid,$newhei){ $imgInfo = getimagesize($src); $imgType = image_type_to_extension($imgInfo[2], false); $fun = "imagecreatefrom{$imgType}"; //声明图片 打开图片 在内存中 $image = $fun($src); //方便配置长度宽度、高度,设置框为变量wid,高度为hei $wid=$imgInfo[0]; $hei=$imgInfo[1]; //判断长度和宽度,以方便等比缩放,规格按照500, 320 if($wid>$hei){ $wid=$newwid; $hei=$newwid/($wid/$hei); }else { $wid = $newhei * ($wid / $hei); $hei = $newhei; } //在内存中建立一张图片 $images2 = imagecreatetruecolor($newwid, $newhei); //建立一个500*320的图片 //将原图复制到新建图片中 //imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) imagecopyresampled($images2, $image, 0, 0, 0, 0, $wid,$hei, $imgInfo[0],$imgInfo[1]); //销毁原始图片 imagedestroy($image); //直接输出图片文件 header("Content-type: ".$imgInfo['mime']); imagejpeg($images2); //保存图片 到新文件 imagejpeg($images2, 'new.jpg', 100); //10代码输出图片的质量 0-100 100质量最高 //销毁 imagedestroy($images2); } imgzip($src,$newwid,$newhei); ?>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP implements tools that can accurately verify ID numbers
phpEncapsulated single file upload class
PHP data method to obtain the interface URL through curl
The above is the detailed content of How to implement proportional scaling of images in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
