- /**
- * Image scaling watermark
- *
- */
- Class cls_photo
- {
- Protected $ waterrate = 0.2; // ratio of the watermark icon to the image
- Protected $ width = 300; // Default width of the thumbnail
- Protected $ height = 200; // default height of the thumbnail
- Protected $ padding = 5; // distance from the watermark image to the edge
- Protected $ water_mark = "./water.png ";
- Protected $ water_mark_pos = 5; // watermark image position (1 = upper left corner, 2 = upper right corner, 3 = lower left corner, 4 = lower right corner, 5 Center)
- Protected $ watermode = 0; // when the thumbnail is 0, no watermark is made. when the thumbnail is 1, the watermark is made.
- Protected $ magick_handle; // Image operation handle
- Protected $ format = array ('jpg ', 'GIF', 'PNG', 'jpeg '); // The format of the image file is limited.
- Protected $ smallpic_mode = 2; // Default mode 0: No thumbnail is generated, 1 is cropping and scaling, 2 is proportional scaling, and 3 is scaling and filling.
- /**
- * Set image parameters
- *
- * @ Param $ arg the image parameters can be put into the array multiple times as follows:
- * @ Param $ protected parameter value
- * Array (
- * 'Waterrate' = & gt; 0.2,
- * 'Water _ mark' => './water.png ',
- * 'Water _ mark_pos '=> 4,
- * 'Smallpic _ mode' => 1
- *);
- * @ Return ture/false
- */
- Public function set_args ($ arg, $ val = "")
- {
- $ Params = array ('waterrate', 'Water _ mark', 'Water _ mark_pos ', 'smallpic _ mode', 'watermode', 'width', 'height ');
- If (is_array ($ arg ))
- {
- Foreach ($ arg as $ k => $ v)
- {
- If (in_array ($ k, $ params ))
- {
- $ This-> $ k = $ v;
- }
- }
- }
- Else
- {
- If (empty ($ val ))
- {
- Return false;
- }
- Else
- {
- If (in_array ($ arg, $ params ))
- {
- $ This-> $ arg = $ val;
- }
- }
- }
- Return true;
- }
- /**
- * Image scaling
- *
- * @ Param $ src_file source file path
- * @ Param $ dst_file path of the target file
- * @ Return the thumbnail path/false
- */
- Public function scale ($ src_file, $ dst_file = "")
- {
- $ Dst_width = $ this-> width;
- $ Dst_height = $ this-> height;
- $ Mode = $ this-> smallpic_mode;
- $ Magic_water_handle = NewMagickWand ();
- If (! MagickReadImage ($ magic_water_handle, $ src_file) return false;
- // Type
- $ Srcext = strtolower (MagickGetImageFormat ($ magic_water_handle ));
- If ($ srcext = 'bmp ')
- {
- $ Srcext = 'jpeg ';
- }
- If (! In_array ($ srcext, $ this-> format) return false;
- // Size
- $ Src_width = MagickGetImageWidth ($ magic_water_handle );
- $ Src_height = MagickGetImageHeight ($ magic_water_handle );
- // Cropping and scaling mode
- If ($ mode = 1)
- {
- $ Pos_x = $ pos_y = 0; // crop the temporary position
- $ Src_widthc = $ src_width; // crop the temporary width.
- $ Src_heightc = $ src_height; // crop the temporary height.
- If ($ src_width/$ src_height> $ dst_width/$ dst_height)
- {
- $ Src_widthc = $ src_height * $ dst_width/$ dst_height;
- $ Pos_x = ($ src_width-$ src_widthc)/2;
- }
- Else
- {
- $ Src_heightc = $ src_width * $ dst_height/$ dst_width;
- $ Pos_y = ($ src_height-$ src_heightc)/2;
- }
- MagickCropImage ($ magic_water_handle, $ src_widthc, $ src_heightc, $ pos_x, $ pos_y); // cropping
- // Because after the MagickCropImage function, the Gif image is changed, but the canvas remains unchanged.
- $ This-> magick_handle = NewMagickWand ();
- MagickNewImage ($ this-> magick_handle, $ src_widthc, $ src_heightc, '# ffff ');
- MagickSetFormat ($ this-> magick_handle, $ srcext );
- MagickCompositeImage ($ this-> magick_handle, $ magic_water_handle, MW_OverCompositeOp, 0, 0 );
- // Zoom
- MagickScaleImage ($ this-> magick_handle, $ dst_width, $ dst_height );
- }
- // Proportional scaling mode
- If ($ mode = 2)
- {
- If ($ src_width/$ src_height> $ dst_width/$ dst_height)
- {
- $ Dst_height = $ dst_width * $ src_height/$ src_width;
- }
- Else
- {
- $ Dst_width = $ dst_height * $ src_width/$ src_height;
- }
- $ This-> magick_handle = $ magic_water_handle; // replace
- MagickScaleImage ($ this-> magick_handle, $ dst_width, $ dst_height); // zoom
- }
- // Scaling and filling mode
- If ($ mode = 3)
- {
- If ($ src_width/$ src_height> $ dst_width/$ dst_height)
- {
- $ Dst_heightc = $ dst_width * $ src_height/$ src_width;
- $ Dst_widthc = $ dst_width;
- }
- Else
- {
- $ Dst_widthc = $ dst_height * $ src_width/$ src_height;
- $ Dst_heightc = $ dst_height;
- }
- MagickScaleImage ($ magic_water_handle, $ dst_widthc, $ dst_heightc); // zoom
- $ This-> magick_handle = NewMagickWand ();
- MagickNewImage ($ this-> magick_handle, $ dst_width, $ dst_height, $ this-> smallpic_bgcolor );
- MagickSetFormat ($ this-> magick_handle, $ srcext );
- MagickCompositeImage ($ this-> magick_handle, $ magic_water_handle, handle, ($ dst_width-$ dst_widthc)/2, ($ dst_height-$ dst_heightc)/2 );
- }
- // Watermark
- If ($ this-> watermode = 1)
- {
- $ This-> set_mark ();
- }
- If (empty ($ dst_file ))
- {
- // Create a temporary file
- $ Dst_file = tempnam ($ _ SERVER ["SINASRV_CACHE_DIR"], "TMP_IMG ");
- }
- MagickWriteImage ($ this-> magick_handle, $ dst_file );
- Return $ dst_file;
- }
- /**
- * Watermark
- *
- * @ Param $ src_file: Path of the image to be watermark
- * @ Param $ dst_file: specifies the path for saving the watermark file. if it is null, a random temporary file is generated.
- * @ Return watermark file path/false
- */
- Public function water_mark ($ src_file, $ dst_file = "")
- {
- $ This-> magick_handle = NewMagickWand ();
- If (! MagickReadImage ($ this-> magick_handle, $ src_file ))
- Return false;
- $ This-> set_mark ();
- If (empty ($ dst_file ))
- {
- // Create a temporary file
- $ Dst_file = tempnam ($ _ SERVER ["SINASRV_CACHE_DIR"], "TMP_IMG ");
- }
- MagickWriteImage ($ this-> magick_handle, $ dst_file );
- Return $ dst_file;
- }
- /**
- * Internal interface
- * Watermarking an image
- *
- */
- Protected function set_mark ()
- {
- // Size
- $ Dst_width = MagickGetImageWidth ($ this-> magick_handle );
- $ Dst_height = MagickGetImageHeight ($ this-> magick_handle );
- // Process the watermark image
- If ($ this-> water_mark & is_file ($ this-> water_mark ))
- {
- $ Magic_water_handle = NewMagickWand ();
- MagickRemoveImage ($ magic_water_handle );
- If (MagickReadImage ($ magic_water_handle, $ this-> water_mark ))
- {
- MagickScaleImage ($ resize, $ dst_width * $ this-> waterrate, $ dst_width * $ this-> waterrate * MagickGetImageHeight ($ resize)/MagickGetImageWidth ($ magic_water_handle )); // scale the watermark to 1/5 of the image
- If ($ this-> water_mark_pos = 1)
- {
- $ Left = $ this-> padding;
- $ Top = $ this-> padding;
- }
- Elseif ($ this-> water_mark_pos = 2)
- {
- $ Left = $ dst_width-$ this-> padding-MagickGetImageWidth ($ magic_water_handle );
- $ Top = $ this-> padding;
- }
- Elseif ($ this-> water_mark_pos = 3)
- {
- $ Left = $ this-> padding;
- $ Top = $ dst_height-$ this-> padding-MagickGetImageHeight ($ magic_water_handle );
- }
- Elseif ($ this-> water_mark_pos = 4)
- {
- $ Left = $ dst_width-$ this-> padding-MagickGetImageWidth ($ magic_water_handle );
- $ Top = $ dst_height-$ this-> padding-MagickGetImageHeight ($ magic_water_handle );
- }
- Elseif ($ this-> water_mark_pos = 5)
- {
- $ Left = ($ dst_width-MagickGetImageWidth ($ magic_water_handle)/2;
- $ Top = ($ dst_height-MagickGetImageHeight ($ magic_water_handle)/2;
- }
- MagickCompositeImage ($ this-> magick_handle, $ magic_water_handle, MW_OverCompositeOp, $ left, $ top );
- }
- }
- }
- }
|