The steps are as follows:
1. Get basic information about the picture
Use this getimagesize function
2. Determine the image resource type and create the corresponding image resource
Use this getPicType function (custom function)
3. Calculate the scaling ratio
The principle is to take the smallest scaling factor. For example: if an 800*600 picture is scaled to less than 200*200, the scaling factor is scale=200/800>200/600?200/600:200/800; because 1/4 < ; 1/3 so choose the scaling factor of 1/4
4. Calculate the scaled size
wdith = floor(800*1/4); round down
height = floor(600*1/4);
As a result, the zoomed image becomes 200*150
5. Create the target image resource
Use this imagecreatetruecolor function to create a true color image
6. Scale proportionally
Use this imagecopyresampled function to achieve scaling
7. Output image
Use this outputImage function (custom function)
8. Release image resources
Use this imagedestroy function
as shown before and after scaling:
750*525
200*140
The code is as follows:
<code><span>/** *<span> @function</span> 等比缩放函数(以保存的方式实现) *<span> @param</span> string $picname 被缩放的处理图片源 *<span> @param</span> int $maxX 缩放后图片的最大宽度 *<span> @param</span> int $maxY 缩放后图片的最大高度 *<span> @param</span> string $pre 缩放后图片名的前缀名 *<span> @return</span> string 返回后的图片名称(带路径),如a.jpg --> s_a.jpg */</span><span><span>function</span><span>scalePic</span><span>(<span>$picname</span>,<span>$maxX</span>=<span>100</span>,<span>$maxY</span>=<span>100</span>,<span>$pre</span>=<span>'s_'</span>)</span> {</span><span>$info</span> = getimagesize(<span>$picname</span>); <span>//获取图片的基本信息</span><span>$width</span> = <span>$info</span>[<span>0</span>];<span>//获取宽度</span><span>$height</span> = <span>$info</span>[<span>1</span>];<span>//获取高度</span><span>//判断图片资源类型并创建对应图片资源</span><span>$im</span> = getPicType(<span>$info</span>[<span>2</span>],<span>$picname</span>); <span>//计算缩放比例</span><span>$scale</span> = (<span>$maxX</span>/<span>$width</span>)>(<span>$maxY</span>/<span>$height</span>)?<span>$maxY</span>/<span>$height</span>:<span>$maxX</span>/<span>$width</span>; <span>//计算缩放后的尺寸</span><span>$sWidth</span> = floor(<span>$width</span>*<span>$scale</span>); <span>$sHeight</span> = floor(<span>$height</span>*<span>$scale</span>); <span>//创建目标图像资源</span><span>$nim</span> = imagecreatetruecolor(<span>$sWidth</span>,<span>$sHeight</span>); <span>//等比缩放</span> imagecopyresampled(<span>$nim</span>,<span>$im</span>,<span>0</span>,<span>0</span>,<span>0</span>,<span>0</span>,<span>$sWidth</span>,<span>$sHeight</span>,<span>$width</span>,<span>$height</span>); <span>//输出图像</span><span>$newPicName</span> = outputImage(<span>$picname</span>,<span>$pre</span>,<span>$nim</span>); <span>//释放图片资源</span> imagedestroy(<span>$im</span>); imagedestroy(<span>$nim</span>); <span>return</span><span>$newPicName</span>; } <span>/** * function 判断并返回图片的类型(以资源方式返回) *<span> @param</span> int $type 图片类型 *<span> @param</span> string $picname 图片名字 *<span> @return</span> 返回对应图片资源 */</span><span><span>function</span><span>getPicType</span><span>(<span>$type</span>,<span>$picname</span>)</span> {</span><span>$im</span>=<span>null</span>; <span>switch</span>(<span>$type</span>) { <span>case</span><span>1</span>: <span>//GIF</span><span>$im</span> = imagecreatefromgif(<span>$picname</span>); <span>break</span>; <span>case</span><span>2</span>: <span>//JPG</span><span>$im</span> = imagecreatefromjpeg(<span>$picname</span>); <span>break</span>; <span>case</span><span>3</span>: <span>//PNG</span><span>$im</span> = imagecreatefrompng(<span>$picname</span>); <span>break</span>; <span>case</span><span>4</span>: <span>//BMP</span><span>$im</span> = imagecreatefromwbmp(<span>$picname</span>); <span>break</span>; <span>default</span>: <span>die</span>(<span>"不认识图片类型"</span>); <span>break</span>; } <span>return</span><span>$im</span>; } <span>/** * function 输出图像 *<span> @param</span> string $picname 图片名字 *<span> @param</span> string $pre 新图片名前缀 *<span> @param</span> resourse $nim 要输出的图像资源 *<span> @return</span> 返回新的图片名 */</span><span><span>function</span><span>outputImage</span><span>(<span>$picname</span>,<span>$pre</span>,<span>$nim</span>)</span> {</span><span>$info</span> = getimagesize(<span>$picname</span>); <span>$picInfo</span> = pathInfo(<span>$picname</span>); <span>$newPicName</span> = <span>$picInfo</span>[<span>'dirname'</span>].<span>'/'</span>.<span>$pre</span>.<span>$picInfo</span>[<span>'basename'</span>];<span>//输出文件的路径</span><span>switch</span>(<span>$info</span>[<span>2</span>]) { <span>case</span><span>1</span>: imagegif(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>2</span>: imagejpeg(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>3</span>: imagepng(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>4</span>: imagewbmp(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; } <span>return</span><span>$newPicName</span>; }</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces the proportional scaling of pictures in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

本站6月2日消息,在目前正在进行的黄仁勋2024台北电脑展主题演讲上,黄仁勋介绍生成式人工智能将推动软件全栈重塑,展示其NIM(NvidiaInferenceMicroservices)云原生微服务。英伟达认为“AI工厂”将掀起一场新产业革命:以微软开创的软件行业为例,黄仁勋认为生成式人工智能将推动其全栈重塑。为方便各种规模的企业部署AI服务,英伟达今年3月推出了NIM(NvidiaInferenceMicroservices)云原生微服务。NIM+是一套经过优化的云原生微服务,旨在缩短上市时间

在PHP中使用switch语句来进行多个分支的选择是很常见的,通常在每个分支结束后会使用break语句来退出switch语句。然而,有些情况下我们不想使用break语句,本文将介绍在PHP switch语句中不使用break的情况。

在php中,break用于跳出当前的语法结构,执行下面的语句;可以在switch、for、while和do while等语句中使用,可以终止循环体的代码并立即跳出当前的循环,执行循环之后的代码。break语句可以带一个参数n,表示跳出循环的层数,如果要跳出多重循环的话,可以用n来表示跳出的层数,如果不带参数默认是跳出本重循环。

在Go语言中,break停止语句用于循环语句中跳出循环,并开始执行循环之后的语句。break语句可以结束for、switch和select的代码块,另外break语句还可以在语句后面添加标签,表示退出某个标签对应的代码块,标签要求必须定义在对应的 for、switch和select的代码块上。

在之前的文章中,我们带大家学习了JS中的几种循环控制结构(while和do-while循环、for循环),下面聊聊跳出循环语句break和continue,希望对大家有所帮助!

说明1、break的作用是跳出现在的循环块(for、while、dowhile)或程序块(switch)。2、循环块的作用是跳出现在循环中的循环体。程序块中的作用是中断和下一个case条件的比较。在switch语句中使用break,终止switch语句。当break用于循环时,跳出循环。在其他地方使用break是没有意义的。实例intsum=0;inti;for(i=1;i

linux param指的是“module_param”函数,其作用是传递命令行参数;在用户态下编程可以通过“main()”的参数来传递命令行参数,而编写一个内核模块则通过“module_param()”来传递参数。

PHP中break语句的作用及注意事项在PHP编程中,break语句是一种控制语句,用于中断循环或switch语句的执行。break语句能够立即跳出当前所处的循环或switch语句,使程序执行流程直接跳转到循环或switch语句后的代码部分,从而提高程序执行的效率和灵活性。在实际编程中,break语句具有重要的作用,但同时也需要注意一些细节和注意事项。一、b


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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