PHP Generate picture Thumbnail class Program _php tutorial

Source: Internet
Author: User
Tags fread imagejpeg unpack
Thumbnails use the most is when we upload a picture to generate a small image, so that it can be a good solution to the image size or affect the overall beauty of the site, the following description of the generated image thumbnail class does not support image upload, we have to upload and then call this class to operate.

Use the following class to generate a picture thumbnail,

The code is as follows Copy Code

Class Resizeimage
{
Type of picture
var $type;
Actual width
var $width;
Actual height
var $height;
The width after the change
var $resize _width;
The height after the change
var $resize _height;
Whether or not to cut the map
var $cut;
Source image
var $srcimg;
Destination image Address
var $dstimg;
Temporarily created images
var $im;

function Resizeimage ($img, $wid, $hei, $c, $dstpath)
{
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
Type of picture

$this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1);

//Initialize image
$this->initi_img (),
//Destination image address
$this, dst_img ($dstpath),
//--
$this->width = Imagesx ($this->im);
$this->height = Imagesy ($this->im);
//Generate Image
$this->newimg ();
Imagedestroy ($this->im);
}
Function newimg ()
{
//Changed image scale
$resize _ratio = ($this->resize_width)/($this->resize_ height);
//Actual image scale
$ratio = ($this->width)/($this->height),
if (($this->cut) = = "1")
//crop
{
if ($ ratio>= $resize _ratio)
//Height first
{
$newimg = Imagecreatetruecolor ($this->resize_width, $this Resize_height);
imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this-

>resize_height, (($this->height) * $resize _ratio), $this->height);
Imagejpeg ($newimg, $this->dstimg);
}
if ($ratio < $resize _ratio)
Width first
{
$newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this-

>resize_height, $this->width, (($this->width)/$resize _ratio));
Imagejpeg ($newimg, $this->dstimg);
}
}
Else
Do not cut the map
{
if ($ratio >= $resize _ratio)
{
$newimg = Imagecreatetruecolor ($this->resize_width, ($this->resize_width)/$ratio);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this-

>resize_width)/$ratio, $this->width, $this->height);
Imagejpeg ($newimg, $this->dstimg);
}
if ($ratio < $resize _ratio)
{
$newimg = Imagecreatetruecolor (($this->resize_height) * $ratio, $this->resize_height);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio,

$this->resize_height, $this->width, $this->height);
Imagejpeg ($newimg, $this->dstimg);
}
}
}
Initializing an image
function initi_img ()
{
if ($this->type== "jpg")
{
$this->im = imagecreatefromjpeg ($this->srcimg);
}
if ($this->type== "gif")
{
$this->im = imagecreatefromgif ($this->srcimg);
}
if ($this->type== "PNG")
{
$this->im = imagecreatefrompng ($this->srcimg);
}
}
Image Destination Address
function Dst_img ($dstpath)
{
$full _length = strlen ($this->srcimg);

$type _length = strlen ($this->type);
$name _length = $full _length-$type _length;


$name = substr ($this->srcimg,0, $name _length-1);
$this->dstimg = $dstpath;


Echo $this->dstimg;
}
}
?>

Method of calling the class

$resizeimage = new Resizeimage ("Image source file Address", "200", "100", "0", "thumbnail address");

Just use the above words, you can generate thumbnails, where the source file and thumbnail address can be the same, the 200,100 represents the width and height

Example 2

PHP thumbnail, such as proportional lossless compression, can fill the blank area supplemental color

The code is as follows Copy Code

Error_reporting (E_all);

Test
Imagezoom (' 1.jpg ', ' 2.jpg ', +, ' #FFFFFF ');

/*
PHP thumbnail functions:
Equal proportional lossless compression, can be filled with supplemental color Author: Andy
Hosting Format:
BMP, JPG, GIF, png
Param
@srcimage: Picture to zoom Out
@dstimage: the picture to save
@dst_width: Reduce the width
@dst_height: Reduce the high
@backgroundcolor: Complementary colors such as: #FFFFFF support 6-bit does not support 3-bit
*/
function Imagezoom ($srcimage, $dstimage, $dst _width, $dst _height, $backgroundcolor) {

The file name is garbled
if (Php_os = = ' WINNT ') {
$srcimage = Iconv (' UTF-8 ', ' GBK ', $srcimage);
$dstimage = Iconv (' UTF-8 ', ' GBK ', $dstimage);
}

$dstimg = Imagecreatetruecolor ($dst _width, $dst _height);
$color = Imagecolorallocate ($dstimg
, Hexdec (substr ($backgroundcolor, 1, 2))
, Hexdec (substr ($backgroundcolor, 3, 2))
, Hexdec (substr ($backgroundcolor, 5, 2))
);
Imagefill ($dstimg, 0, 0, $color);

if (! $arr =getimagesize ($srcimage)) {
echo "The file to generate the thumbnail does not exist";
Exit
}

$SRC _width = $arr [0];
$SRC _height = $arr [1];
$srcimg = null;
$method = Getcreatemethod ($srcimage);
if ($method) {
Eval (' $srcimg = '. $method. ';' );
}

$DST _x = 0;
$DST _y = 0;
$DST _w = $dst _width;
$DST _h = $dst _height;
if (($dst _width/$dst _height-$src _width/$src _height) > 0) {
$DST _w = $src _width * ($dst _height/$src _height);
$DST _x = ($dst _width-$dst _w)/2;
} elseif (($dst _width/$dst _height-$src _width/$src _height) < 0) {
$DST _h = $src _height * ($dst _width/$src _width);
$DST _y = ($dst _height-$dst _h)/2;
}

Imagecopyresampled ($dstimg, $srcimg, $dst _x
, $dst _y, 0, 0, $dst _w, $dst _h, $src _width, $src _height);

//Save format
$arr = array (
' jpg ' = ' imagejpeg '
, ' jpeg ' = ' imagejpeg '
, ' png ' = ' imagepng '
, ' gif ' = ' imagegif '
, ' bmp ' = ' imagebmp '
);
$suffix = Strtolower (Array_pop (Explode ('. ', $dstimage) ) );
if (!in_array ($suffix, Array_keys ($arr))) {
echo "saved filename error";
exit;
} else {
eval ($arr [$suffix]. ' ($dstimg, "'. $dstimage. '); ');
}

imagejpeg ($dstimg, $dstimage);

Imagedestroy ($DSTIMG);
Imagedestroy ($SRCIMG);

}


function Getcreatemethod ($file) {
$arr = Array (
' 474946 ' = ' imagecreatefromgif (' $file ') '
, ' ffd8ff ' = ' imagecreatefromjpeg (' $file ') '
, ' 424D ' = ' imagecreatefrombmp (' $file ') '
, ' 89504E ' = ' imagecreatefrompng (' $file ') '
);
$FD = fopen ($file, "RB");
$data = Fread ($FD, 3);

$data = Str2hex ($data);

if (Array_key_exists ($data, $arr)) {
return $arr [$data];
} elseif (Array_key_exists (substr ($data, 0, 4), $arr)) {
return $arr [substr ($data, 0, 4)];
} else {
return false;
}
}

function Str2hex ($STR) {
$ret = "";

for ($i = 0; $i < strlen ($STR); $i + +) {
$ret. = Ord ($str [$i]) >= 16? Strval (Dechex (Ord ($str [$i]))
: ' 0 '. Strval (Dechex (Ord ($str [$i]));
}

Return Strtoupper ($ret);
}

BMP Create function PHP itself has no
function Imagecreatefrombmp ($filename)
{
if (! $f 1 = fopen ($filename, "RB")) return FALSE;

$FILE = Unpack ("Vfile_type/vfile_size/vreserved/vbitmap_offset", Fread ($f 1,14));
if ($FILE [' file_type ']! = 19778) return FALSE;

$BMP = Unpack (' Vheader_size/vwidth/vheight/vplanes/vbits_per_pixel '.
'/vcompression/vsize_bitmap/vhoriz_resolution '.
'/vvert_resolution/vcolors_used/vcolors_important ', fread ($f 1,40));
$BMP [' colors '] = POW (2, $BMP [' bits_per_pixel ']);
if ($BMP [' size_bitmap '] = = 0) $BMP [' size_bitmap '] = $FILE [' file_size ']-$FILE [' Bitmap_offset '];
$BMP [' bytes_per_pixel '] = $BMP [' Bits_per_pixel ']/8;
$BMP [' bytes_per_pixel2 '] = ceil ($BMP [' bytes_per_pixel ']);
$BMP [' decal '] = ($BMP [' width ']* $BMP [' Bytes_per_pixel ']/4];
$BMP [' decal ']-= Floor ($BMP [' width ']* $BMP [' Bytes_per_pixel ']/4];
$BMP [' decal '] = ($BMP [' decal ']);
if ($BMP [' decal '] = = 4) $BMP [' decal '] = 0;

$PALETTE = Array ();
if ($BMP [' Colors '] < 16777216)
{
$PALETTE = Unpack (' V '. $BMP [' colors '], fread ($f 1, $BMP [' Colors ']*4)];
}

$IMG = Fread ($f 1, $BMP [' Size_bitmap ']);
$VIDE = chr (0);

$res = Imagecreatetruecolor ($BMP [' width '], $BMP [' height ']);
$P = 0;
$Y = $BMP [' Height ']-1;
while ($Y >= 0)
{
$X = 0;
while ($X < $BMP [' width '])
{
if ($BMP [' bits_per_pixel '] = = 24)
$COLOR = Unpack ("V", substr ($IMG, $P, 3). $VIDE);
ElseIf ($BMP [' bits_per_pixel '] = = 16)
{
$COLOR = Unpack ("n", substr ($IMG, $P, 2));
$COLOR [1] = $PALETTE [$COLOR [1]+1];
}
ElseIf ($BMP [' bits_per_pixel '] = = 8)
{
$COLOR = Unpack ("n", $VIDE. substr ($IMG, $P, 1));
$COLOR [1] = $PALETTE [$COLOR [1]+1];
}
ElseIf ($BMP [' bits_per_pixel '] = = 4)
{
$COLOR = Unpack ("n", $VIDE. substr ($IMG, Floor ($P), 1));
if ($P *)%2 = = 0) $COLOR [1] = ($COLOR [1] >> 4); else $COLOR [1] = ($COLOR [1] & 0x0F);
$COLOR [1] = $PALETTE [$COLOR [1]+1];
}
ElseIf ($BMP [' bits_per_pixel '] = = 1)
{
$COLOR = Unpack ("n", $VIDE. substr ($IMG, Floor ($P), 1));
if ($P *8)%8 = = 0) $COLOR [1] = $COLOR [1] >>7;
ElseIf (($P *8)%8 = = 1) $COLOR [1] = ($COLOR [1] & 0x40) >>6;
ElseIf (($P *8)%8 = = 2) $COLOR [1] = ($COLOR [1] & 0x20) >>5;
ElseIf (($P *8)%8 = = 3) $COLOR [1] = ($COLOR [1] & 0x10) >>4;
ElseIf (($P *8)%8 = = 4) $COLOR [1] = ($COLOR [1] & 0x8) >>3;
ElseIf (($P *8)%8 = = 5) $COLOR [1] = ($COLOR [1] & 0x4) >>2;
ElseIf (($P *8)%8 = = 6) $COLOR [1] = ($COLOR [1] & 0x2) >>1;
ElseIf (($P *8)%8 = = 7) $COLOR [1] = ($COLOR [1] & 0x1);
$COLOR [1] = $PALETTE [$COLOR [1]+1];
}
Else
return FALSE;
Imagesetpixel ($res, $X, $Y, $COLOR [1]);
$X + +;
$P + = $BMP [' Bytes_per_pixel '];
}
$Y--;
$P + = $BMP [' decal '];
}
Fclose ($f 1);

return $res;
}
BMP save function, PHP itself is not
function Imagebmp ($im, $FN = False)
{
if (! $im) return false;

if ($fn = = = False) $fn = ' php://output ';
$f = fopen ($fn, "w");
if (! $f) return false;

$biWidth = Imagesx ($im);
$biHeight = Imagesy ($im);
$biBPLine = $biWidth * 3;
$biStride = ($biBPLine + 3) & ~;
$biSizeImage = $biStride * $biHeight;
$bfOffBits = 54;
$bfSize = $bfOffBits + $biSizeImage;

Fwrite ($f, ' BM ', 2);
Fwrite ($f, Pack (' VvvV ', $bfSize, 0, 0, $bfOffBits));

Fwrite ($f, Pack (' VVVVVVVVVVV ', Max, $biWidth, $biHeight, 1, 0, $biSizeImage, 0, 0, 0, 0));

$numpad = $biStride-$biBPLine;
for ($y = $biHeight-1; $y >= 0;--$y)
{
for ($x = 0; $x < $biWidth; + + $x)
{
$col = Imagecolorat ($im, $x, $y);
Fwrite ($f, Pack (' V ', $col), 3);
}
for ($i = 0; $i < $numpad; + + $i)
Fwrite ($f, Pack (' C ', 0));
}
Fclose ($f);
return true;
}

?>

To summarize, the first class file is not as good as the second one, because the picture will be a little blurred after the first build, and the resulting class is high quality later.

http://www.bkjia.com/PHPjc/632959.html www.bkjia.com true http://www.bkjia.com/PHPjc/632959.html techarticle thumbnails Use the most is when we upload a picture to generate a small image, so that it can be a good solution to the image size or affect the overall beauty of the site, the following description of the generation of pictures shrink ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.