Skip to content

Commit c65b305

Browse files
authored
Revision and optimization
Fill transparen background image duplicate code optimization destroy destination image
1 parent 94ffc69 commit c65b305

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

library/imgcopyresized.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,23 @@ public function generateThumbnail($file = array(), $savepath = "/")
153153
// height is smaller than x:y
154154
$new_Y = $final_height;
155155
$new_X = floor($final_height * ($org_X / $org_Y)); // Resize based on height
156-
$dst_image = imagecreatetruecolor($new_X, $new_Y);
157-
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $new_X, $new_Y, $org_X, $org_Y);
158156
}
159157
else
160158
{
161159
// height is heigher than x:y
162160
$new_X = $final_width;
163161
$new_Y = floor($final_width * ($org_Y / $org_X)); // Resize based on width
164-
$dst_image = imagecreatetruecolor($new_X, $new_Y);
165-
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $new_X, $new_Y, $org_X, $org_Y);
166162
}
167163
}
164+
165+
// Create truecolor image
166+
$dst_image = imagecreatetruecolor($new_X, $new_Y);
167+
if ($extension === "png" || $extension === "gif")
168+
{ // Fill transparen background image
169+
$whitecolor = imagecolorallocate($dst_image, 255, 255, 255);
170+
imagefill($dst_image, 0, 0, $whitecolor);
171+
}
172+
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $new_X, $new_Y, $org_X, $org_Y);
168173

169174
// Create thumbnail image
170175
if ($extension === "jpg" || $extension === "jpeg")
@@ -188,6 +193,8 @@ public function generateThumbnail($file = array(), $savepath = "/")
188193
imagecopyresampled($tmp_dst_image, $src_image, 0, 0, 0, 0, $final_width, $final_height, $final_width, $final_height);
189194

190195
imagejpeg($tmp_dst_image, $SavePathThump, $this->final_quality); // Output image to file
196+
197+
imagedestroy($dst_image); // Destroy destination image
191198
imagedestroy($tmp_dst_image); // Destroy temporary image
192199

193200
// After successful completion, this class returns the src string of the generated thumbnail ( example string="/img/1372_image.jpg" )

0 commit comments

Comments
 (0)