PHP Plus watermark Proportional Scaling Program

Source: Internet
Author: User
Tags imagejpeg

PHP Plus watermark Proportional Scaling Program
function watermark ($DESIMG, $WATERIMG, $positon =1, $saveas =false, $alpha =30)

011 {

012//Get the basic information of the image

013 $temp =pathinfo ($DESIMG);

014 $name = $temp ["basename"];//<span Class=t_tag onclick=tagshow (event) href= "Tag.php?name=%ce%c4%bc%fe" > Files </SPAN> Name

015 $path = $temp [dirname]];//file Folder

016 $extension = $temp ["extension"];//file name extension

017 if ($saveas)

018 {

019//need to Save as

020 $name =rtrim ($name, ". $extension"). " _2. ";/ /re-renaming

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

022}

023 Else

024 {

025///No need to save as to overwrite the original artwork

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

027}

028 $info =getimageinfo ($DESIMG);//Get information about the target picture

029 $info 2=getimageinfo ($WATERIMG);//Get the information of the watermark picture

030

031 $desImg =create ($DESIMG);//create from original artwork

032 $waterImg =create ($WATERIMG);//create from watermark picture

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]-$info 2[0];

043 $y = 0;

044}

045//Position 3: Center

046 if ($positon ==3)

047 {

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

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

050}

051//Position 4: Bottom Left

052 if ($positon ==4)

053 {

054 $x = 0;

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

056}

057//Position 5: Bottom Right

058 if ($positon ==5)

059 {

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

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

062}

063 Imagecopymerge ($DESIMG, $WATERIMG, $x, $y, 0,0, $info 2[0], $info 2[1], $alpha);

064 imagejpeg ($DESIMG, $savepath);

065 Imagedestroy ($DESIMG);

066 Imagedestroy ($WATERIMG);

067 return $savepath;

068}

069/**

070 * Get the information of the picture, Width,height,image/type

071 * @param string $src picture path

072 * @return <span class=t_tag onclick=tagshow (event) href= "Tag.php?name=%ca%fd%d7%e9" > Array </SPAN>

073 * **/

074 function Getimageinfo ($SRC)

075 {

076 return getimagesize ($SRC);

077}

078/**

079 * Create picture, return resource type

080 * @param string $src picture path

081 * @return Resource $im return 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 picture path

@param int $w thumbnail width

* @param int $h thumbnail height

@return Mixed returns the thumbnail path

106 * **/

107

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

109 {

$temp =pathinfo ($SRC);

$name = $temp ["basename"];//filename

112 $dir = $temp [dirname]];//file Folder

113 $extension = $temp ["extension"];//file name extension

114 $savepath = "{$dir}/{$name}.thumb.jpg";//thumbnail save path, new file name is *.thumb.jpg

115

116//Get the basic information of the picture

117 $info =getimageinfo ($SRC);

118 $width = $info [0];//get picture width

119 $height = $info [1];//get picture height

$per 1=round ($width/$height, 2);//Calculate the length/width ratio of the original artwork

121 $per 2=round ($w/$h, 2);//calculate the thumbnail aspect ratio

122

123//Calculate scaling ratio

124 if ($per 1> $per 2| | $per 1== $per 2)

125 {

126//The original image aspect ratio is greater than or equal to the thumbnail aspect ratio, then the width first

127 $per = $w/$width;

128}

129 if ($per 1< $per 2)

130 {

131//The original length and width ratio is less than the thumbnail aspect ratio, it is high priority

132 $per = $h/$height;

133}

134 $temp _w=intval ($width * $per);//Calculate the width of the original image scaling

135 $temp _h=intval ($height * $per);//Calculate the height after the original image is scaled

136 $temp _img=imagecreatetruecolor ($temp _w, $temp _h);//Create Canvas

137 $im =create ($SRC);

138 imagecopyresampled ($temp _img, $im, 0,0,0,0, $temp _w, $temp _h, $width, $height);

139 if ($per 1> $per 2)

140 {

Imagejpeg ($temp _img, $savepath);

addbg ($savepath, $w, $h, "w");

143//Width first, fill up the background when the height is insufficient after zooming

144}

145 if ($per 1== $per 2)

146 {

147 imagejpeg ($temp _img, $savepath);

148 return $savepath;

149//Equal ratio scaling

150}

151 if ($per 1< $per 2)

152 {

153 imagejpeg ($temp _img, $savepath);

154

addbg return ($savepath, $w, $h, "H");

156//High priority, make up the background when the width is insufficient after zooming

157}

158}

159/**

160 * Add background

161 * @param string $src picture 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 high priority wh: equal ratio

165 * @return return picture with background

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, 0,0, $white);//Fill background

172

173//Get the target picture information

174 $info =getimageinfo ($SRC);

175 $width = $info [0];//target picture width

176 $height = $info [1];//target picture height

177 $img =create ($SRC);

178 if ($fisrt = = "WH")

179 {

180//Equal ratio scaling

181 return $SRC;

182}

183 Else

184 {

185 if ($fisrt = = "W")

186 {

187 $x = 0;

188 $y = ($h-$height)/2;//Vertical Center

189}

190 if ($fisrt = = "H")

191 {

$x = ($w-$width)/2;//Horizontal Center

193 $y = 0;

194}

195 Imagecopymerge ($BG, $img, $x, $y, 0,0, $width, $height, 100);

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.