diff options
author | Malf Furious <m@lfurio.us> | 2018-10-27 18:28:22 -0400 |
---|---|---|
committer | Malf Furious <m@lfurio.us> | 2018-10-27 18:28:22 -0400 |
commit | 0421aa1b60f4fe6bf140888159c58059c1013588 (patch) | |
tree | c3285276f6c53b6789e2f6dc82cb3b0fd17b38a4 /app/class/image.php | |
parent | 495157341d60522084dcc9f6219877b6ba497312 (diff) | |
parent | 6512655aee73d3d295daa4de0e4ef25c08cfec9e (diff) | |
download | scrott-0.1.tar.gz scrott-0.1.zip |
Merge branch 'rel/v0.1'v0.1
Diffstat (limited to 'app/class/image.php')
-rw-r--r-- | app/class/image.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/app/class/image.php b/app/class/image.php new file mode 100644 index 0000000..6b73cae --- /dev/null +++ b/app/class/image.php @@ -0,0 +1,70 @@ +<?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_WRITE_FUNCS['image/jpeg'] = "imagejpeg"; +$_IMG_WRITE_FUNCS['image/jpg'] = "imagejpeg"; +$_IMG_WRITE_FUNCS['image/png'] = "imagepng"; + +/* + * 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; +} + +?> |