<?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; } ?>