PHP-image compression

Source: Internet
Author: User
Tags imagejpeg
PHP-Image Compression Method 1:

 $ Dw) {$ Par = $ Dw/$ width; $ width = $ Dw; $ height = $ height * $ Par; IF ($ height> $ Dh) {$ Par = $ Dh/$ height; $ height = $ Dh; $ width = $ width * $ Par;} ElseIF ($ height> $ Dh) {$ Par = $ Dh/$ height; $ height = $ Dh; $ width = $ width * $ Par; IF ($ width> $ Dw) {$ Par = $ Dw/$ width; $ width = $ Dw; $ height = $ height * $ Par ;}} Else {$ width = $ width; $ height = $ height ;}$ nImg = ImageCreateTrueColor ($ width, $ height); # Create a true color canvas ImageCopyReSampled ($ nImg, $ Img, 0, 0, $ width, $ height, $ w, $ h); # ressample and copy some images and resize ImageJpeg ($ nImg, $ Image ); # Return True to output the image to a browser or file in JPEG format; # if a thumbnail is generated,} Else {$ w = ImagesX ($ Img ); $ h = ImagesY ($ Img); $ width = $ w; $ height = $ h; $ nImg = ImageCreateTrueColor ($ Dw, $ Dh ); IF ($ h/$ w> $ Dh/$ Dw) {# The height is relatively large $ width = $ Dw; $ height = $ h * $ Dw/$ w; $ IntNH = $ height-$ Dh; ImageCopyReSampled ($ nImg, $ Img, 0,-$ IntNH/1.8, 0, 0, $ Dw, $ height, $ w, $ h);} Else {# relatively wide $ height = $ Dh; $ width = $ w * $ Dh/$ h; $ IntNW = $ width-$ Dw; imagecopyresampleds ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, 0, $ width, $ Dh, $ w, $ h);} ImageJpeg ($ nImg, $ Image); Return True ;}}?>
 

Method 2:

PHP image compression class
 Array ("output" => "imagejpeg"), "gif" => array ("output" => "imagegif "), "png" => array ("output" => "imagepng"), "wbmp" => array ("output" => "image2wbmp "), "jpeg" => array ("output" => "imagejpeg");/*** constructor */function ThumbHandler () {$ this-> mask_font_color = "# ffffff"; $ this-> font = 2; $ this-> font_size = 12 ;} /*** retrieve the image width */function getImgWidth ($ src) {return imagesx ($ src);}/*** retrieve the image height */function getImgHeigh T ($ src) {return imagesy ($ src);}/*** sets the image generation path ** @ param string $ src_img image generation path */function setSrcImg ($ src_img, $ img_type = null) {if (! File_exists ($ src_img) {die ("image does not exist");} if (! Empty ($ img_type) {$ this-> img_type = $ img_type;} else {$ this-> img_type = $ this-> _ getImgType ($ src_img );} $ this-> _ checkValid ($ this-> img_type); // The file_get_contents function requires the php version> 4.3.0 $ src = ''; if (function_exists (" file_get_contents ")) {$ src = file_get_contents ($ src_img);} else {$ handle = fopen ($ src_img, "r"); while (! Feof ($ handle) {$ src. = fgets ($ fd, 4096) ;}fclose ($ handle) ;}if (empty ($ src) {die ("Image source is empty ");} $ this-> h_src = @ ImageCreateFromString ($ src); $ this-> src_w = $ this-> getImgWidth ($ this-> h_src ); $ this-> src_h = $ this-> getImgHeight ($ this-> h_src );} /*** set the image generation path ** @ param string $ dst_img image generation path */function setDstImg ($ dst_img) {$ arr = explode ('/', $ dst_img ); $ last = array_pop ($ arr); $ path = implode ('/', $ Arr); $ this-> _ mkdirs ($ path); $ this-> dst_img = $ dst_img ;} /*** set the image display quality ** @ param string $ n quality */function setImgDisplayQuality ($ n) {$ this-> img_display_quality = (int) $ n ;} /*** set the image generation quality ** @ param string $ n quality */function setImgCreateQuality ($ n) {$ this-> img_create_quality = (int) $ n ;} /*** set the text watermark ** @ param string $ word watermark text * @ param integer $ font watermark font * @ param string $ color watermark font color */function setMa SkWord ($ word) {$ this-> mask_word. = $ word;}/*** set the font color ** @ param string $ color font color */function setMaskFontColor ($ color = "# ffffff ") {$ this-> mask_font_color = $ color;}/*** set the watermark font ** @ param string | integer $ font */function setMaskFont ($ font = 2) {if (! Is_numeric ($ font )&&! File_exists ($ font) {die ("the font file does not exist") ;}$ this-> font = $ font ;}/ *** set the text font size, valid only for truetype fonts */function setMaskFontSize ($ size = "12") {$ this-> font_size = $ size ;} /*** set the image watermark ** @ param string $ img watermark image source */function setMaskImg ($ img) {$ this-> mask_img = $ img ;} /*** set the watermark horizontal offset ** @ param integer $ x horizontal offset */function setMaskOffsetX ($ x) {$ this-> mask_offset_x = (int) $ x ;} /*** set the watermark vertical offset ** @ param integer $ y vertical offset Volume */function setMaskOffsetY ($ y) {$ this-> mask_offset_y = (int) $ y;}/*** specify the watermark position ** @ param integer $ position, 1: upper left, 2: lower left, 3: upper right, 0/4: lower right */function setMaskPosition ($ position = 0) {$ this-> mask_position = (int) $ position ;} /*** set the image merging degree ** @ param integer $ n merging degree */function setMaskImgPct ($ n) {$ this-> mask_img_pct = (int) $ n ;} /*** set the text merging degree ** @ param integer $ n merging degree */function setMaskTxtPct ($ n) {$ this-> mask_t Xt_pct = (int) $ n;}/*** set the thumbnail border ** @ param (type) (parameter name) (description) */function setDstImgBorder ($ size = 1, $ color = "#000000") {$ this-> img_border_size = (int) $ size; $ this-> img_border_color = $ color ;} /***** horizontal flip */function flipH () {$ this-> _ flip_x ++;}/*** vertical flip */function flipV () {$ this-> _ flip_y ++;}/*** sets the cut type ** @ param (type) (parameter name) (description) */function setCutType ($ type) {$ this-> cut_type = (int) $ type;}/*** set Image cropping ** @ Param integer $ width rectangular cut */function setRectangleCut ($ width, $ height) {$ this-> fill_w = (int) $ width; $ this-> fill_h = (int) $ height;}/*** set the starting coordinate point of the source image cut ** @ param (type) (parameter name) (description) */function setSrcCutPosition ($ x, $ y) {$ this-> src_x = (int) $ x; $ this-> src_y = (int) $ y ;}/ *** create an image, main function * @ param integer $ a when the second parameter is missing, this parameter is used as a percentage, * otherwise, it is used as the width value * @ param integer $ B. The scaled height of the image */function createImg ($ a, $ B = null) {$ num = f Unc_num_args (); if (1 = $ num) {$ r = (int) $ a; if ($ r <1) {die ("image scaling ratio cannot be less than 1") ;}$ this-> img_scale = $ r; $ this-> _ setNewImgSize ($ r );} if (2 = $ num) {$ w = (int) $ a; $ h = (int) $ B; if (0 = $ w) {die ("target width cannot be 0");} if (0 = $ h) {die ("target height cannot be 0 ");} $ this-> _ setNewImgSize ($ w, $ h);} if ($ this-> _ flip_x % 2! = 0) {$ this-> _ flipH ($ this-> h_src);} if ($ this-> _ flip_y % 2! = 0) {$ this-> _ flipV ($ this-> h_src);} $ this-> _ createMask (); $ this-> _ output (); // release if (imagedestroy ($ this-> h_src) & imagedestroy ($ this-> h_dst) {Return true;} else {Return false ;}} /*** generate the watermark. two methods are called to generate the watermark text and watermark image. */function _ createMask () {if ($ this-> mask_word) {// Obtain the font information $ this-> _ setFontInfo (); if ($ this-> _ isFull () {die ("the watermark text is too large ");} else {$ this-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> d St_h); $ white = ImageColorAllocate ($ this-> h_dst, 255,255,255); imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white); // fill in the background color $ this-> _ drawBorder (); imagecopyresampled ($ this-> h_dst, $ this-> h_src, $ this-> start_x, $ this-> start_y, $ this-> src_x, $ this-> src_y, $ this-> fill_w, $ this-> fill_h, $ this-> copy_w, $ this-> copy_h); $ this-> _ createMaskWord ($ this-> h_dst) ;}} if ($ this-> mask_img) {$ t His-> _ loadMaskImg (); // Obtain the width and height if ($ this-> _ isFull () when loading ()) {// Copy the watermark generated on the source image $ this-> _ createMaskImg ($ this-> h_src); $ this-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h); $ white = ImageColorAllocate ($ this-> h_dst, 255,255,255); imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white); // fill in the background color $ this-> _ drawBorder (); imagecopyresampled ($ this-> h_dst, $ this-> h_src, $ this-> start_x, $ this-> Start_y, $ this-> src_x, $ this-> src_y, $ this-> fill_w, $ this-> start_y, $ this-> copy_w, $ this-> copy_h );} else {// create a new graph and copy $ this-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h); $ white = ImageColorAllocate ($ this-> h_dst, 255,255,255); imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white ); // fill in the background color $ this-> _ drawBorder (); imagecopyresampled ($ this-> h_dst, $ this-> h_src, $ this-> sta Rt_x, $ this-> start_y, $ this-> src_x, $ this-> src_y, $ this-> fill_w, $ this-> fill_h, $ this-> copy_w, $ this-> copy_h); $ this-> _ createMaskImg ($ this-> h_dst) ;}} if (empty ($ this-> mask_word) & amp; empty ($ this-> mask_img) {$ this-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h ); $ white = ImageColorAllocate ($ this-> h_dst, 255,255,255); imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white ); // Fill in the background color $ this-> _ drawBorder (); imagecopyresampled ($ this-> h_dst, $ this-> h_src, $ this-> start_x, $ this-> start_y, $ this-> src_x, $ this-> src_y, $ this-> fill_w, $ this-> fill_h, $ this-> copy_w, $ this-> copy_h );}} /*** draw border */function _ drawBorder () {if (! Empty ($ this-> img_border_size) {$ c = $ this-> _ parseColor ($ this-> img_border_color); $ color = ImageColorAllocate ($ this-> h_src, $ c [0], $ c [1], $ c [2]); imagefilledrectangle ($ this-> h_dst, 0,0, $ this-> dst_w, $ this-> dst_h, $ color); // fill in the background color }}

  

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.