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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?php
/*
* SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to UNLICENSE
*/
/*
* This file defines custom image manipulation routines for Scrott. All
* are available in the global namespace.
*/
/*
* Mappings from image MIME types to PHP open/write functions
*/
$_IMG_OPEN_FUNCS['image/jpeg'] = "imagecreatefromjpeg";
$_IMG_OPEN_FUNCS['image/jpg'] = "imagecreatefromjpeg";
$_IMG_OPEN_FUNCS['image/png'] = "imagecreatefrompng";
$_IMG_OPEN_FUNCS['image/gif'] = "imagecreatefromgif";
$_IMG_OPEN_FUNCS['image/bmp'] = "imagecreatefrombmp";
$_IMG_OPEN_FUNCS['image/x-ms-bmp'] = "imagecreatefrombmp";
$_IMG_WRITE_FUNCS['image/jpeg'] = "imagejpeg";
$_IMG_WRITE_FUNCS['image/jpg'] = "imagejpeg";
$_IMG_WRITE_FUNCS['image/png'] = "imagepng";
$_IMG_WRITE_FUNCS['image/gif'] = "imagegif";
$_IMG_WRITE_FUNCS['image/bmp'] = "imagebmp";
$_IMG_WRITE_FUNCS['image/x-ms-bmp'] = "imagebmp";
/*
* Open the given image and crop it, such that the result is a square
* image whose side length is equal to the smaller of the original
* dimensions and whose area is centered on the area of the original
* image. The resulting image is written to the same path as provided,
* overwriting the image.
*/
function imageSquareCrop(string $uri) : bool
{
global $_IMG_OPEN_FUNCS;
global $_IMG_WRITE_FUNCS;
$mime = mime_content_type($uri);
$img = $_IMG_OPEN_FUNCS[$mime]($uri);
if ($img === false)
return false;
$wid = imagesx($img);
$hei = imagesy($img);
$rec = array();
$rec['width'] = min($wid, $hei);
$rec['height'] = $rec['width'];
$rec['y'] = ($hei / 2) - ($rec['height'] / 2);
$rec['x'] = ($wid / 2) - ($rec['width'] / 2);
$cropped = imagecrop($img, $rec);
imagedestroy($img);
if ($cropped === false)
return false;
$ret = $_IMG_WRITE_FUNCS[$mime]($cropped, $uri);
imagedestroy($cropped);
return $ret;
}
?>
|