Php proportional scaling program such as watermarking-PHP source code

Source: Internet
Author: User
Tags imagejpeg
Ec (2); php proportional scaling program such as adding watermarks functionwatermark ($ desImg, $ waterImg, $ positon1, $ saveasfalse, $ alpha30) & nbsp; 011 {& nbsp; 012 get the basic information of the Target Image & nbsp; 013 $ temppathinfo ($ desImg); & nbsp; 014 $ name $ temp [& quot; ba script ec (2 ); script

Php proportional scaling program such as Watermarking
Function watermark ($ desImg, $ waterImg, $ positon = 1, $ saveas = false, $ alpha = 30)

011 {

012 // obtain basic object information

013 $ temp = pathinfo ($ desImg );

014 $ name = $ temp ["basename"]; //FileName

015 $ path = $ temp ["dirname"]; // folder where the file is located

016 $ extension = $ temp ["extension"]; // File extension

017 if ($ saveas)

018 {

019 // save

020 $ name = rtrim ($ name, ". $ extension"). "_ 2."; // rename

021 $ savepath = $ path. "/". $ name. $ extension;

022}

023 else

024 {

025 // overwrite the source image without saving it

026 $ savepath = $ path. "/". $ name;

027}

028 $ info = getImageInfo ($ desImg); // obtain the Target Image Information

029 $ info2 = getImageInfo ($ waterImg); // obtain the watermark image information

030

031 $ desImg = create ($ desImg); // create from source Image

032 $ waterImg = create ($ waterImg); // create a watermark image

033 // position 1: Top left

034 if ($ positon = 1)

035 {

036 $ x = 0;

037 $ y = 0;

038}

039 // position 2: Top right

040 if ($ positon = 2)

041 {

042 $ x = $ info [0]-$ info2 [0];

043 $ y = 0;

044}

045 // position 3: Center

046 if ($ positon = 3)

047 {

048 $ x = ($ info [0]-$ info2 [0])/2;

049 $ y = ($ info [1]-$ info2 [1])/2;

050}

051 // position 4: Bottom left

052 if ($ positon = 4)

053 {

054 $ x = 0;

055 $ y = $ info [1]-$ info2 [1];

056}

057 // position 5: Bottom Right

058 if ($ positon = 5)

059 {

060 $ x = $ info [0]-$ info2 [0];

061 $ y = $ info [1]-$ info2 [1];

062}

063 imagecopymerge ($ desImg, $ waterImg, $ x, $ y, 0, $ info2 [0], $ info2 [1], $ alpha );

064 imagejpeg ($ desImg, $ savepath );

065 imagedestroy ($ desImg );

066 imagedestroy ($ waterImg );

067 return $ savepath;

068}

069 /**

070 * obtain image information, width, height, image/type

071 * @ param string $ src image path

072 * @ returnArray

073 ***/

074 function getImageInfo ($ src)

075 {

076 return getimagesize ($ src );

077}

078 /**

079 * create an image and return the resource type

080 * @ param string $ src image path

081 * @ return resource $ im returned resource Type

082 ***/

083 function create ($ src)

084 {

085 $ info = getImageInfo ($ src );

086 switch ($ info [2])

087 {

088 case 1:

089 $ im = imagecreatefromgif ($ src );

090 break;

091 case 2:

092 $ im = imagecreatefromjpeg ($ src );

093 break;

094 case 3:

095 $ im = imagecreatefrompng ($ src );

096 break;

097}

098 return $ im;

099}

100 /**

101 * thumbnail Main Function

102 * @ param string $ src image path

103 * @ param int $ w thumbnail width

104 * @ param int $ h thumbnail height

105 * @ return mixed return the thumbnail path

106 ***/

107

108 function resize ($ src, $ w, $ h)

109 {

110 $ temp = pathinfo ($ src );

111 $ name = $ temp ["basename"]; // file name

112 $ dir = $ temp ["dirname"]; // folder where the file is located

113 $ extension = $ temp ["extension"]; // File extension

114 $ savepath = "{$ dir}/your saved name).thumb.jpg"; // saved path of the thumbnail. The new file name is * .thumb.jpg.

115

116 // obtain basic image information

117 $ info = getImageInfo ($ src );

118 $ width = $ info [0]; // obtain the Image width

119 $ height = $ info [1]; // obtain the Image height

120 $ per1 = round ($ width/$ height, 2); // calculate the aspect ratio of the source Image

121 $ per2 = round ($ w/$ h, 2); // calculate the thumbnail Aspect Ratio

122

123 // calculate the scaling ratio

124 if ($ per1> $ per2 | $ per1 = $ per2)

125 {

126 // the aspect ratio of the source image is greater than or equal to the aspect ratio of the thumbnail.

127 $ per = $ w/$ width;

128}

129 if ($ per1 <$ per2)

130 {

131 // If the aspect ratio of the source image is smaller than the aspect ratio of the thumbnail, the height prevails.

132 $ per = $ h/$ height;

133}

134 $ temp_w = intval ($ width * $ per); // calculate the scaled width of the source image.

135 $ temp_h = intval ($ height * $ per); // calculate the scaled height of the source image.

136 $ temp_img = imagecreatetruecolor ($ temp_w, $ temp_h); // create a canvas

137 $ im = create ($ src );

138 imagecopyresampled ($ temp_img, $ im, $ temp_w, $ temp_h, $ width, $ height );

139 if ($ per1> $ per2)

140 {

141 imagejpeg ($ temp_img, $ savepath );

142 return addBg ($ savepath, $ w, $ h, "w ");

143 // The width is prioritized, and the background is added when the height is insufficient after scaling.

144}

145 if ($ per1 = $ per2)

146 {

147 imagejpeg ($ temp_img, $ savepath );

148 return $ savepath;

149 // proportional Scaling

150}

151 if ($ per1 <$ per2)

152 {

153 imagejpeg ($ temp_img, $ savepath );

154

155 return addBg ($ savepath, $ w, $ h, "h ");

156 // height first. Fill in the background when the width is insufficient after scaling.

157}

158}

159 /**

160 * Add background

161 * @ param string $ src image path

162 * @ param int $ w background image width

163 * @ param int $ h Background Image Height

164 * @ param String $ first determines the final position of the image, w width first h height first wh: Proportional Ratio

165 * @ return returns the image with the background added.

166 ***/

167 function addBg ($ src, $ w, $ h, $ fisrt = "w ")

168 {

169 $ bg = imagecreatetruecolor ($ w, $ h );

170 $ white = imagecolorallocate ($ bg, 255,255,255 );

171 imagefill ($ bg, $ white); // fill the background

172

173 // obtain the Target Image Information

174 $ info = getImageInfo ($ src );

175 $ width = $ info [0]; // Target Image width

176 $ height = $ info [1]; // Target Image height

177 $ img = create ($ src );

178 if ($ fisrt = "wh ")

179 {

180 // proportional Scaling

181 return $ src;

182}

183 else

184 {

185 if ($ fisrt = "w ")

186 {

187 $ x = 0;

188 $ y = ($ h-$ height)/2; // center vertically

189}

190 if ($ fisrt = "h ")

191 {

192 $ x = ($ w-$ width)/2; // horizontally centered

193 $ y = 0;

194}

195 imagecopymerge ($ bg, $ img, $ x, $ y, 100, $ width, $ height );

196 imagejpeg ($ bg, $ src );

197 imagedestroy ($ bg );

198 imagedestroy ($ img );

199 return $ src;

200}

201

202}

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.