PHP Image Processing-create a thumbnail using magicwand (watermarks are supported)

Source: Internet
Author: User
During program development, users often encounter the need to upload images to generate small thumbnails. Because the length and width ratios of the images uploaded by users are different, it is a problem to display the small images neatly on the page when they are generated proportionally.
Considering that proportional scaling is required for images, it is better to add the edge whitelist when displaying images. As follows:

Figure 1 dimension 480x350

Figure 2 dimension 270x360

If we set the size of the generated image to 100x100, the two images are generated as follows:

In this way, although the source image is a long and wide image, we can still generate non-deformed thumbnails and display them neatly on the page.
Principle:Create a new background image, scale the original image to the center of the background size, and generate a thumbnail with the required uniform size.
I always like conciseness. modules with simple processing functions prefer functions rather than classes.
PHP generates such a small thumbnail function as follows:

<? PHP
/*************************************** ********************

* Author ziming (jorygong@gmail.com)

* This function extracts the image from the source file and scales down proportionally. It outputs the image with the specified length and width to the target file.
* If the size of the source image is smaller than the specified size, the background color is added;
* Source File Format: GIF, JPG, and PNG
* Target file format: Same-source file format
* $ Srcfile: source file (*. jpg/*. gif/*. PNG)
* $ Dstdir: target file directory (relative to the domain name storage directory)
* $ Dstfilename: target file name (must not contain an extension. The same extension of the same source file is automatically added)
* $ Width: The target image width.
* $ Height: Target Image Height
* $ Bgcolor: fill color of the scaled image background
* $ Watermark: watermark image file name. If it is null, no watermark is added.
* $ Position: watermark image position (1 = upper left corner, 2 = upper right corner, 3 = lower left corner, 4 = lower right corner)
* $ Padding: watermark image distance from edge
* An array of target file information is returned when the file is successful.
* Return array ("FILENAME" => image path and name, "dirname" => image directory, "basename" => image name (including extension ), "extension" => image extension );
* Return an empty array return array () when a failure occurs ();

**************************************** ********************/

Function resize_image ($ srcfile, $ dstdir, $ dstfilename, $ width, $ height, $ bgcolor = "# ffffff", $ watermark = "", $ position = 4, $ padding = 20)
{
$ Return_info = array ();
If (! Is_file ($ srcfile ))
{
Return $ return_info;
}
$ Size = @ getimagesize ($ srcfile );

// Size
$ Srcw = $ size [0];
$ Srch = $ size [1];
$ Srcr = $ srcw/$ srch;
$ Dstr = $ width/$ height;
If ($ width <= 0 | $ height <= 0)
{
$ NEWW = $ srcw; // The height of the new image.
$ Newh = $ srch; // width of the new image
}
Else
{
If ($ width> $ srcw)
{
If ($ height> $ srch)
{
$ NEWW = $ srcw;
$ Newh = $ srch;
}
Else
{
$ Newh = $ height;
$ NEWW = round ($ newh * $ srcr );
}
}
Else
{
If ($ dstr> $ srcr)
{
$ Newh = $ height;
$ NEWW = round ($ newh * $ srcr );
}
Else
{
$ NEWW = $ width;
$ Newh = round ($ NEWW/$ srcr );
}
}
}
$ Newl = round ($ width-$ NEWW)/2); // left margin of the new image
$ Newt = round ($ height-$ newh)/2); // top margin of the new image
If ($ newl <0)
{
$ Newl = 0;
}
If ($ Newt <0)
{
$ Newt = 0;
}

// Type
Switch ($ size [2])
{
Case 1:
$ Im = @ imagecreatefromgif ($ srcfile );
$ Extension. = "GIF ";
Break;
Case 2:
$ Im = @ imagecreatefromjpeg ($ srcfile );
$ Extension. = "jpg ";
Break;
Case 3:
$ Im = @ imagecreatefrompng ($ srcfile );
$ Extension. = "PNG ";
Break;
Default:
Return $ ret_info;
}

// Scale and merge the source image to the background image
$ BGR = hexdec (substr ($ bgcolor, 1, 2 ));
$ BGG = hexdec (substr ($ bgcolor, 3, 2 ));
$ BGB = hexdec (substr ($ bgcolor, 5, 2 ));
If (@ function_exists ("imagecopyresampled "))
{
$ Newim = @ imagecreatetruecolor ($ width, $ height );
$ Back = @ imagecolorallocate ($ newim, $ BGR, $ BGG, $ BGB );
@ Imagefilledrectangle ($ newim, 0, 0, $ width-1, $ height-1, $ back );
@ Imagecopyresampled ($ newim, $ im, $ newl, $ Newt, 0, 0, $ NEWW, $ newh, $ srcw, $ srch );
}
Else
{
$ Newim = imagecreate ($ width, $ height );
@ Imagecolorallocate ($ newim, $ BGR, $ BGG, $ BGB );
Imagecopyresized ($ newim, $ im, $ newl, $ Newt, 0, 0, $ NEWW, $ newh, $ srcw, $ srch );
}

// Process the watermark image
If ($ watermark)
{
$ Padding = intval ($ padding );
$ Wmsize = @ getimagesize ($ watermark );
If ($ wmsize)
{
If ($ position = 1)
{
$ WML = $ padding;
$ WMT = $ padding;
}
Elseif ($ position = 2)
{
$ WML = $ width-$ padding-$ wmsize [0];
$ WMT = $ padding;
}
Elseif ($ position = 3)
{
$ WML = $ padding;
$ WMT = $ height-$ padding-$ wmsize [1];
}
Else
{
$ WML = $ width-$ padding-$ wmsize [0];
$ WMT = $ height-$ padding-$ wmsize [1];
}
Switch ($ wmsize [2])
{
Case 1:
$ Wim = @ imagecreatefromgif ($ watermark );
Break;
Case 2:
$ Im = @ imagecreatefromjpeg ($ watermark );
Break;
Case 3:
$ Im = @ imagecreatefrompng ($ watermark );
Break;
Default:
Return $ ret_info;
}
@ Imagecopy ($ newim, $ Wim, $ WML, $ WMT, 0, 0, $ wsize [0], $ wsize [1]);
}
}

$ Dstfilename. = ".". $ extension;
Switch ($ size [2])
{
Case 1:
Imagegif ($ newim, $ dstfilename );
Break;
Case 2:
Imagejpeg ($ newim, $ dstfilename );
Break;
Case 3:
Imagepng ($ newim, $ dstfilename );
Break;
}

$ Return_info ["FILENAME"] = $ dstdir. $ dstfilename;
$ Return_info ["dirname"] = $ dstdir;
$ Return_info ["basename"] = $ dstfilename;
$ Return_info ["extension"] = $ extension;
Return $ return_info;
}

?>

We recommend that you use the PNG format to create a watermark with a high definition.
The above function requires PHP to support the GD library and the target folder for storing thumbnails has been created.
However, using the GD library to generate thumbnails poses a serious problem, that is, processing photos taken by some mobile phones or cameras (these photos can be called incomplete data or the format is not in line with the standard image) when the memory occupied by PHP exceeds the maximum limit on the server, generating a black image or directly failing.
Fortunately, another professional image processing tool, imagemagic, can be used for faster processing and easy processing of incomplete images.
The magickwand thumbnail generation function with better functions is as follows:

<? PHP
/*************************************** ********************

* Author ziming (jorygong@gmail.com)

* This function extracts the image from the source file and scales down proportionally. It outputs the image with the specified length and width to the target file.
* If the size of the source image is smaller than the specified size, the background color is added;
* Source File Format: GIF, JPG, and PNG
* Target file format: Same-source file format
* $ Srcfile: source file (*. jpg/*. gif/*. PNG)
* $ Dstdir: target file directory (relative to the domain name storage directory)
* $ Dstfilename: target file name (must not contain an extension. The same extension of the same source file is automatically added)
* $ Width: The target image width.
* $ Height: Target Image Height
* $ Bgcolor: fill color of the scaled image background
* $ Watermark: watermark image file name. If it is null, no watermark is added.
* $ Position: watermark image position (1 = upper left corner, 2 = upper right corner, 3 = lower left corner, 4 = lower right corner)
* $ Padding: watermark image distance from edge
* An array of target file information is returned when the file is successful.
* Return array ("FILENAME" => image path and name, "dirname" => image directory, "basename" => image name (including extension ), "extension" => image extension );
* Return an empty array return array () when a failure occurs ();

**************************************** ********************/

Function resize_image ($ srcfile, $ dstdir, $ dstfilename, $ width, $ height, $ bgcolor = "# ffffff", $ watermark = "", $ position = 4, $ padding = 20)
{
$ Return_info = array ();
If (! Is_file ($ srcfile ))
{
Return $ return_info;
}
$ Mymagickwand = newmagickwand ();
If (! Magickreadimage ($ mymagickwand, $ srcfile ))
{
Return $ return_info;
}

// Size
$ Srcw = magickgetimagewidth ($ mymagickwand );
$ Srch = magickgetimageheight ($ mymagickwand );
$ Srcr = $ srcw/$ srch;
$ Dstr = $ width/$ height;
If ($ width <= 0 | $ height <= 0)
{
$ NEWW = $ srcw; // The height of the new image.
$ Newh = $ srch; // width of the new image
}
Else
{
If ($ width> $ srcw)
{
If ($ height> $ srch)
{
$ NEWW = $ srcw;
$ Newh = $ srch;
}
Else
{
$ Newh = $ height;
$ NEWW = round ($ newh * $ srcr );
}
}
Else
{
If ($ dstr> $ srcr)
{
$ Newh = $ height;
$ NEWW = round ($ newh * $ srcr );
}
Else
{
$ NEWW = $ width;
$ Newh = round ($ NEWW/$ srcr );
}
}
}
$ Newl = round ($ width-$ NEWW)/2); // left margin of the new image
$ Newt = round ($ height-$ newh)/2); // top margin of the new image
If ($ newl <0)
{
$ Newl = 0;
}
If ($ Newt <0)
{
$ Newt = 0;
}

// Type
$ Srct = magickgetimageformat ($ mymagickwand );
If ($ srct = "Jpeg ")
{
$ Extension = "jpg ";
}
Elseif ($ srct = "GIF ")
{
$ Extension = "GIF ";
}
Elseif ($ srct = "PNG ")
{
$ Extension = "PNG ";
}
Else
{
Return $ return_info;
}

// Generate the background image
$ Bgmagickwand = newmagickwand ();
Magicknewimage ($ bgmagickwand, $ width, $ height, $ bgcolor );
Magicksetformat ($ bgmagickwand, $ srct );

// Scale and merge the source image to the background image
Magickscaleimage ($ mymagickwand, $ NEWW, $ newh );
Magickcompositeimage ($ bgmagickwand, $ mymagickwand, mw_overcompositeop, $ newl, $ Newt );

// Process the watermark image
If ($ watermark & is_file ($ watermark ))
{
Magickremoveimage ($ mymagickwand );
$ Padding = intval ($ padding );
If (magickreadimage ($ mymagickwand, $ watermark ))
{
If ($ position = 1)
{
$ WML = $ padding;
$ WMT = $ padding;
}
Elseif ($ position = 2)
{
$ WML = $ width-$ padding-magickgetimagewidth ($ mymagickwand );
$ WMT = $ padding;
}
Elseif ($ position = 3)
{
$ WML = $ padding;
$ WMT = $ height-$ padding-magickgetimageheight ($ mymagickwand );
}
Else
{
$ WML = $ width-$ padding-magickgetimagewidth ($ mymagickwand );
$ WMT = $ height-$ padding-magickgetimageheight ($ mymagickwand );
}
Magickcompositeimage ($ bgmagickwand, $ mymagickwand, mw_overcompositeop, $ WML, $ WMT );
}
}

$ Dstfilename. = ".". $ extension;
Magickwriteimage ($ bgmagickwand, $ dstfilename );
Destroymagickwand ($ mymagickwand );
Destroymagickwand ($ bgmagickwand );

$ Return_info ["FILENAME"] = $ dstdir. $ dstfilename;
$ Return_info ["dirname"] = $ dstdir;
$ Return_info ["basename"] = $ dstfilename;
$ Return_info ["extension"] = $ extension;
Return $ return_info;
}

?>

Just like the current Phillips DVD machine, all the broken discs and pirated discs can be read, which is superior to correction.
The thumbnail image generated by using this function is not deformed, and the length and width are specified. The page will not be messy because the ratio of the thumbnail length and width is the same as that of the source image and the ratio of the width and length of each thumbnail is different.
PHP Image Processing-magicwand source code for generating thumbnails download:

Download the file (9 times already downloaded) Click here to download the file: function_img.rar

Now that magicwand is mentioned, let's load the magicwand extension library of PhP5 in windows:
1. Download the DLL file and put it in the extension_dir directory of PHP. Download files (6 times already downloaded)Click here to download the file magickwand_php5_dll.rar.

2. Open the PhP5 configuration file and add the following three lines.

Extension = php_magickwand_dyn.dll
Extension = php_magickwand_q8_st.dll
Extension = php_magickwand_q16_st.dll

3. Save and restart the Apache server.

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.