使用PHP缩略图跟剪切图
使用PHP缩略图和剪切图
API:
resource imagecreatetruecolor ( int $width , int $height )
magecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
是否定义了本函数取决于 PHP 和 GD 的版本。从 PHP 4.0.6 到 4.1.x 只要加载了 GD 模块本函数一直存在,但是在没有安装 GD2 的时候调用,PHP 将发出致命错误并退出。在 PHP 4.2.x 中此行为改为发出警告而不是错误。其它版本只在安装了正确的 GD 版本时定义了本函数。
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
imagejpeg() 从 image 图像以 filename 为文件名创建一个 JPEG 图像。
ool imagedestroy ( resource $image )
imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。
http://www.cnblogs.com/xiaomia/archive/2010/11/13/1876191.html
一开始采用了 imagecopyresized 方法进行图像等比缩小,实际操作后发现,图像缩小后燥点非常严重。后再换用 imagecopysampled 方法,该方法会对图像进行重新采样,对缩小的图像进行平滑处理,使清晰度得到很大提高
list($src_w,$src_h)=getimagesize($src_img); // 获取原图尺寸 $dst_scale = $dst_h/$dst_w; //目标图像长宽比 $src_scale = $src_h/$src_w; // 原图长宽比 if($src_scale>=$dst_scale){ // 过高 $w = intval($src_w); $h = intval($dst_scale*$w); $x = 0; $y = ($src_h - $h)/3; } else{ // 过宽 $h = intval($src_h); $w = intval($h/$dst_scale); $x = ($src_w - $w)/2; $y = 0; } // 剪裁 $source=imagecreatefromjpeg($src_img); $croped=imagecreatetruecolor($w, $h); imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h); // 缩放 $scale = $dst_w/$w; $target = imagecreatetruecolor($dst_w, $dst_h); $final_w = intval($w*$scale); $final_h = intval($h*$scale); imagecopyresampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h); // 保存 $timestamp = time(); imagejpeg($target, "$timestamp.jpg"); imagedestroy($target);
http://www.cnblogs.com/analyzer/articles/1267017.html
先说说缩略图,它用得比较多,代码如下:
<?php header("Content-type: image/png"); //原图 $filename='source.jpg'; //缩放比例 新图/原图 $percent = '0.5'; list($width,$height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; // Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); // Resize imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Output imagepng($thumb); ?>
再说说剪切图,就是不缩放,而是从原图中剪切出一块小图,比较个性。代码如下:
<?php $maxW=300; $maxH=300; //图片路径 $link= "big.jpg"; $img = imagecreatefromjpeg($link); list($width, $height, $type, $attr) = getimagesize($link); $widthnum=ceil($width/$maxW); $heightnum=ceil($height/$maxH); $iOut = imagecreatetruecolor ($maxW,$maxH); //bool imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h ) //将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。 //整图循环切割 for ($i=0;$i < $heightnum;$i++) { for ($j=0;$j < $widthnum;$j++) { imagecopy($iOut,$img,0,0,($j*$maxW),($i*$maxH),$maxW,$maxH);//复制图片的一部分 imagejpeg($iOut,"images/".$i."_".$j.".jpg"); //输出成0_0.jpg,0_1.jpg这样的格式 } } //只剪切一个开始部位的小图.复制图片的一部分 imagecopy($iOut,$img,0,0,0,0,$maxW,$maxH); imagejpeg($iOut,"images/sm.jpg"); ?>

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

src and href are respectively, 1. src is the abbreviation of source, which is used to specify the path of external resources. It is usually used to embed external files, such as pictures, audios, videos, etc. The src attribute is generally used on img, script, iframe and other tags. ; 2. href is the abbreviation of hypertext reference, which is used to specify the path of the target resource of the hyperlink. It is usually used to link to external documents or other pages. The href attribute is generally used on tags such as a and link.

Detailed explanation of the method of converting int type to byte in PHP In PHP, we often need to convert the integer type (int) to the byte (Byte) type, such as when dealing with network data transmission, file processing, or encryption algorithms. This article will introduce in detail how to convert the int type to the byte type and provide specific code examples. 1. The relationship between int type and byte In the computer field, the basic data type int represents an integer, while byte (Byte) is a computer storage unit, usually 8-bit binary data

In C++, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods. Implicit conversions The compiler performs implicit type conversions automatically. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things

The value range of int32 is from -2 to the 31st power to 2 to the 31st power minus 1, that is, -2147483648 to 2147483647. int32 is a signed integer type, which means it can represent positive numbers, negative numbers, and zero. It uses 1 bit to represent the sign bit, and the remaining 31 bits are used to represent the numerical value. Since one bit is used to represent the sign bit, the effective number of int32 bits is 31.

In Java, int is a 32-bit signed data type, and its variables require 32-bit memory; the valid range of the int data type is -2147483648 to 2147483647, and all integers in this range are called integer literals. An integer literal can be assigned to an int variable, such as "int num1 = 21;".

The number of bytes occupied by the int type may vary in different programming languages and different hardware platforms. Detailed introduction: 1. In C language, the int type usually occupies 2 bytes or 4 bytes. In 32-bit systems, the int type occupies 4 bytes, while in 16-bit systems, the int type occupies 2 bytes. In a 64-bit system, the int type may occupy 8 bytes; 2. In Java, the int type usually occupies 4 bytes, while in Python, the int type has no byte limit and can be automatically adjusted, etc.

Methods to convert string to int type: 1. Use the built-in function int(); 2. Use try-except to handle exceptions; 3. Use regular expressions.

Conversion method: 1. Use the Itoa() function, the syntax "strconv.Itoa(num)"; 2. Use the FormatInt() function to convert int type data into the specified base and return it in the form of a string, the syntax "strconv .FormatInt(num,10)".
