PHP Build-Share _php tutorials with thumbnail classes and custom functions

Source: Internet
Author: User
Tags file copy imagejpeg
A total of two equal proportional thumbnail methods can be used for reference
One, for the class file, after instantiation can be used
Second, for the custom method, relatively lightweight

class file
Copy CodeThe code is as follows: $resizeimage = new Resizeimage ("./shawn.jpg", "200", "100", "0", "... /pic/shawnsun.jpg ");
Instantiate the following class to generate a thumbnail image
Where the source file and thumbnail address can be the same, 200,100 represents the width and height, the fourth parameter is optional 0 not, 1 is

Copy CodeThe code is as follows: <?php
Class resizeimage{

Type of picture
Public $type;
Actual width
Public $width;
Actual height
Public $height;
The width after the change
Public $resize _width;
The height after the change
Public $resize _height;
Whether or not to cut the map
Public $cut;
Source image
Public $srcimg;
Destination image Address
Public $dstimg;
Temporarily created images
Public $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);
Initializing an image
$this->initi_img ();
Destination image Address
$this->dst_img ($dstpath);
W & H
$this->width = imagesx ($this->im);
$this->height = Imagesy ($this->im);
Creating images
$this->newimg ();
Imagedestroy ($this->im);
}

function newimg () {

The proportions of the altered image
$resize _ratio = ($this->resize_width)/($this->resize_height);
Ratio of actual images
$ratio = ($this->width)/($this->height);

if (($this->cut) = = "1")
Crop chart
{
if ($ratio >= $resize _ratio)
High priority
{
$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;
}
}

?>

Custom Methods
Copy the Code code as follows:
Thumbs (' Shawn.jpg ', ' shawnsun.jpg ', 100,100);
Parameter properties are similar to method one

Copy CodeThe code is as follows:
<?php

function thumbs ($FileName, $SaveTo, $SetW, $SetH) {
$IMGInfo = getimagesize ($FileName);
if (! $IMGInfo) return false;

if ($IMGInfo [' MIME ']== ' image/pjpeg ' | | $IMGInfo [' MIME ']== ' Image/jpeg ') {
$ThisPhoto = Imagecreatefromjpeg ($FileName);
}elseif ($IMGInfo [' MIME ']== ' image/x-png ' | | $IMGInfo [' MIME ']== ' Image/png ') {
$ThisPhoto = Imagecreatefrompng ($FileName);
}elseif ($IMGInfo [' MIME ']== ' Image/gif ') {
$ThisPhoto =imagecreatefromgif ($FileName);
}

$width = $IMGInfo [0];
$height = $IMGInfo [1];
$scalc = max ($width/$SetW, $height/$SetH);
$NW = Intval ($width/$scalc);
$NH = Intval ($height/$scalc);
echo "Thumbnail size: w$nw,h$nh
";

if ($SetW-$NW = = 1) {$nw = $SetW;}
if ($SetH-$nh = = 1) {$nh = $SetH;}
echo "+ Fixed 1 MP: W$NW,H$NH
";

Fill width
if ($SetW-$nw > 0) {
$NH = $nh + (($NH/$NW) * ($SetW-$NW));
echo "* Required width". ($SetW-$NW). ", accompany to fill high." (($NH/$NW) * ($SetW-$NW)). "
";
$NW = $SetW;
}
Fill High
if ($SetH-$nh > 0) {
$NW = $nw + (($NW/$NH) * ($SetH-$NH));
echo "* Need to be high". ($SetH-$nh). ", accompany the width." (($NW/$NH) * ($SetH-$NH)). "
";
$NH = $SetH;
}
$NW = Intval ($NW);
$NH = Intval ($NH);
echo "+ Fix Size: w$nw,h$nh
";

$PX = ($SetW-$NW)/2;
$py = ($SetH-$NH)/2;
echo "Window Size: W$setw,h$seth
";
echo "+ Offset FIX: x$px,y$py
";

$NewPhoto =imagecreatetruecolor ($SetW, $SetH);
Imagecopyresized ($NewPhoto, $ThisPhoto, $px, $py, 0,0, $NW, $nh, $width, $height);
Imagejpeg ($NewPhoto, $SaveTo);
return true;
}

?>

http://www.bkjia.com/PHPjc/824811.html www.bkjia.com true http://www.bkjia.com/PHPjc/824811.html techarticle a total of two equal-scale thumbnail method can be borrowed from a, for the class file, after instantiation can be used two, for the custom method, the comparison of lightweight type file copy code code as follows: $resi ...

  • 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.