This article mainly analyzes how to process images in equal proportions in PHP. Friends who need it can refer to it. I hope it can help everyone. Let’s take a look with the editor below.
<? // 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白 function my_image_resize($src_file, $dst_file , $new_width , $new_height) { $new_width= intval($new_width); $new_height=intval($new_width); if($new_width <1 || $new_height <1) { echo "params width or height error !"; exit(); } if(!file_exists($src_file)) { echo $src_file . " is not exists !"; exit(); } // 图像类型 $type=exif_imagetype($src_file); $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF); if(!in_array($type, $support_type,true)) { echo "this type of image does not support! only support jpg , gif or png"; exit(); } //Load image switch($type) { case IMAGETYPE_JPEG : $src_img=imagecreatefromjpeg($src_file); break; case IMAGETYPE_PNG : $src_img=imagecreatefrompng($src_file); break; case IMAGETYPE_GIF : $src_img=imagecreatefromgif($src_file); break; default: echo "Load image error!"; exit(); } $w=imagesx($src_img); $h=imagesy($src_img); $ratio_w=1.0 * $new_width / $w;//源图像与目标图像宽的比 $ratio_h=1.0 * $new_height / $h; $ratio=1.0; // 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了) if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) { if($ratio_w < $ratio_h) { $ratio = $ratio_h ; // 情况一,宽度的比例比高度方向的小,按照高度的比例标准来裁剪或放大 }else { $ratio = $ratio_w ; } // 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求 $inter_w=(int)($new_width / $ratio); $inter_h=(int) ($new_height / $ratio); $inter_img=imagecreatetruecolor($inter_w , $inter_h); //var_dump($inter_img); imagecopy($inter_img, $src_img, 0,0,0,0,$inter_w,$inter_h); // 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像 // 定义一个新的图像 $new_img=imagecreatetruecolor($new_width,$new_height); //var_dump($new_img);exit(); imagecopyresampled($new_img,$inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h); switch($type) { case IMAGETYPE_JPEG : imagejpeg($new_img, $dst_file,100); // 存储图像 break; case IMAGETYPE_PNG : imagepng($new_img,$dst_file,100); break; case IMAGETYPE_GIF : imagegif($new_img,$dst_file,100); break; default: break; } } else { // end if 1 目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪 // =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) ) $ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //取比例大的那个值 // 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大 $inter_w=(int)($w * $ratio); $inter_h=(int) ($h * $ratio); $inter_img=imagecreatetruecolor($inter_w , $inter_h); //将原图缩放比例后裁剪 imagecopyresampled($inter_img,$src_img,0,0,0,0,$inter_w,$inter_h,$w,$h); // 定义一个新的图像 $new_img=imagecreatetruecolor($new_width,$new_height); imagecopy($new_img, $inter_img, 0,0,0,0,$new_width,$new_height); switch($type) { case IMAGETYPE_JPEG : imagejpeg($new_img, $dst_file,100); // 存储图像 break; case IMAGETYPE_PNG : imagepng($new_img,$dst_file,100); break; case IMAGETYPE_GIF : imagegif($new_img,$dst_file,100); break; default: break; } }// if3}// end function my_image_resize('1.jpg','111.jpg','100px','100px'); ?>
Related recommendations:
How to simply implement responsive image scaling
Processing page loading image js (equal proportion compression image)
The above is the detailed content of How to process images in equal proportions in php. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

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