-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaestroImages.php
More file actions
51 lines (45 loc) · 2.14 KB
/
Copy pathMaestroImages.php
File metadata and controls
51 lines (45 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
class MaestroImages
{
public function resize($input_image, $new_width, $new_height, $quality, $output_image)
{
list($width, $height) = getimagesize($input_image);
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($input_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output the scaled image
imagejpeg($image_p, $output_image, $quality);
}
public function crop($input_image, $x, $y, $w, $h, $quality, $output_image)
{
$image = imagecreatefromjpeg($input_image);
$image = @imagecrop($image, ['x' => $x, 'y' => $y, 'width' => $w, 'height' => $h]);
imagejpeg($image, $output_image, $quality);
}
public function mask($input_image, $mask_path, $quality, $output_image)
{
$mask_path = imagecreatefrompng($mask_path);
$input_image = imagecreatefromjpeg($input_image);
$xSize = imagesx($input_image);
$ySize = imagesy($input_image);
$newPicture = imagecreatetruecolor($xSize, $ySize);
imagesavealpha($newPicture, true);
imagefill($newPicture, 0, 0, imagecolorallocatealpha($newPicture, 0, 0, 0, 127));
if ($xSize != imagesx($mask_path) || $ySize != imagesy($mask_path)) {
$tempPic = imagecreatetruecolor($xSize, $ySize);
imagecopyresampled($tempPic, $mask_path, 0, 0, 0, 0, $xSize, $ySize, imagesx($mask_path), imagesy($mask_path));
imagedestroy($mask_path);
$mask_path = $tempPic;
}
for ($x = 0; $x < $xSize; $x++) {
for ($y = 0; $y < $ySize; $y++) {
$alpha = imagecolorsforindex($mask_path, imagecolorat($mask_path, $x, $y));
$alpha = 127 - floor($alpha['red'] / 2);
$color = imagecolorsforindex($input_image, imagecolorat($input_image, $x, $y));
imagesetpixel($newPicture, $x, $y, imagecolorallocatealpha($newPicture, $color['red'], $color['green'], $color['blue'], $alpha));
}
}
imagepng($newPicture, $output_image, $quality);
}
}