Questions about PHP generating thumbnail images

Source: Internet
Author: User
Tags imagejpeg
Problems with PHP generating thumbnail images
I think so ....
I set the thumbnail image size to be generated to 100*50

My original picture is 1000*600.

I want to have the original picture equal to the scale, horizontal or vertical center. Other places with white fill, do not know what I mean to express no

------Solution--------------------
Original W1 H1
New Figure W2 H2

If W1/H1 > W2/H2//low height after scaling
New Figure High H = h2/w2*w1
Offset y = (h2-h)/2 when vertically centered
x = 0
If W1/H1 < W2/H2//low width after scaling
New Figure W = w2/h2*h1
Offset at horizontal Center x = (x2-x)/2
y = 0


------Solution--------------------
Picture processing class for a CMS
PHP Code
 
  __construct ($targetfile, $cfg _thumb, $cfg _watermarktext, $photo _waterpos, $photo _diaphaneity, $photo _wheight, $photo    _wwidth, $cfg _watermarktype, $photo _marktrans, $trueMarkimg, $attach); }//Destructors function __construct ($targetfile, $cfg _thumb, $cfg _watermarktext, $photo _waterpos, $photo _diaphaneity, $ph Oto_wheight, $photo _wwidth, $cfg _watermarktype, $photo _marktrans, $trueMarkimg, $attach = Array ()) {$this->th        Umbstatus = $cfg _thumb;        $this->watermarktext = $cfg _watermarktext;        $this->watermarkstatus = $photo _waterpos;        $this->watermarkquality = $photo _marktrans;        $this->watermarkminwidth = $photo _wwidth;        $this->watermarkminheight = $photo _wheight;        $this->watermarktype = $cfg _watermarktype;        $this->watermarktrans = $photo _diaphaneity;        $this->animatedgif = 0;        $this->targetfile = $targetfile;        $this->attachinfo = @getimagesize ($targetfile);  $this->attach = $attach;      Switch ($this->attachinfo[' mime ') {case ' image/jpeg ': $this->imagecreatefrom Func = function_exists (' Imagecreatefromjpeg ')?                ' Imagecreatefromjpeg ': '; $this->imagefunc = function_exists (' imagejpeg ')?                ' imagejpeg ': ';            Break Case ' image/gif ': $this->imagecreatefromfunc = function_exists (' imagecreatefromgif ')?                ' Imagecreatefromgif ': '; $this->imagefunc = function_exists (' imagegif ')?                ' Imagegif ': ';            Break Case ' image/png ': $this->imagecreatefromfunc = function_exists (' imagecreatefrompng ')?                ' Imagecreatefrompng ': '; $this->imagefunc = function_exists (' imagepng ')?                ' Imagepng ': ';        Break }//a function of match type does not exist $this->attach[' size ' = Empty ($this->attach[' size ')?        @filesize ($targetfile): $this->attach[' size '; if ($this->attachinfo[' mime ') = = ' Image/gif '{$fp = fopen ($targetfile, ' RB ');            $targetfilecontent = Fread ($fp, $this->attach[' size ');            Fclose ($FP); $this->animatedgif = Strpos ($targetfilecontent, ' NETSCAPE2.0 ') = = = False?        0:1; }}/** * Generate thumbnails * * @access public * @param int $thumbwidth picture width * @param int $th Umbheight picture Height * @param int $preview preview * @return void */function thumb ($thumbwidth, $thumbhei        ght, $preview = 0) {$this->thumb_gd ($thumbwidth, $thumbheight, $preview); if ($this->thumbstatus = = 2 && $this->watermarkstatus) {$this->image ($this->targetfi            Le, $this->attach);        $this->attach[' size '] = filesize ($this->targetfile);     }}/** * Generate thumbnails using GD * * @access public * @param int $thumbwidth picture Width * @param int $thumbheight picture Height * @param int $preview Preview * @return void */function thumb_gd ($thumbwidth, $thumbheight, $preview = 0) {if ($this->thumbstatus && function_exists (' Imagecreatetruecolor ') && function_exists (' imagecopyresampled ') &&            Function_exists (' imagejpeg ')) {$imagecreatefromfunc = $this->imagecreatefromfunc; $imagefunc = $this->thumbstatus = = 1?            ' imagejpeg ': $this->imagefunc;            List ($imagewidth, $imageheight) = $this->attachinfo;                if (! $this->animatedgif && ($imagewidth >= $thumbwidth | | $imageheight >= $thumbheight)) {                $attach _photo = $imagecreatefromfunc ($this->targetfile);                $x _ratio = $thumbwidth/$imagewidth;                $y _ratio = $thumbheight/$imageheight; if (($x _ratio * $imageheight) < $thumbheight) {$thumb [' height '] = ceil ($x _ratio * $im                    Ageheight); $thumb [' width '] = $thumbwidTh                    } else {$thumb [' width '] = ceil ($y _ratio * $imagewidth);                $thumb [' height '] = $thumbheight; } $targetfile =! $preview? ($this->thumbstatus = = 1? $this->targetfile.                Thumb.jpg ': $this->targetfile): './watermark_tmp.jpg ';                $thumb _photo = Imagecreatetruecolor ($thumb [' width '], $thumb [' height ']); Imagecopyresampled ($thumb _photo, $attach _photo, 0, 0, 0, 0, $thumb [' width '], $thumb [' height '], $imagewidth, $                ImageHeight);  if ($this->attachinfo[' mime '] = = ' Image/jpeg ') {$imagefunc ($thumb _photo, $targetfile,                100);                } else {$imagefunc ($thumb _photo, $targetfile); } $this->attach[' thumb ' = $this->thumbstatus = = 1?            1:0; }}}}//end Class
  • 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.