{"id":13912656,"url":"https://github.com/gumlet/php-image-resize","last_synced_at":"2025-05-14T02:09:26.841Z","repository":{"id":8163211,"uuid":"9585024","full_name":"gumlet/php-image-resize","owner":"gumlet","description":"PHP library to resize, scale and crop images. Cloud solution available at: www.gumlet.com","archived":false,"fork":false,"pushed_at":"2025-04-24T04:43:51.000Z","size":393,"stargazers_count":1198,"open_issues_count":6,"forks_count":313,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-05-03T04:39:44.579Z","etag":null,"topics":["crop","php","php-library","resize","scale"],"latest_commit_sha":null,"homepage":"https://www.gumlet.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gumlet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2013-04-21T20:11:02.000Z","updated_at":"2025-04-28T14:19:29.000Z","dependencies_parsed_at":"2024-01-14T08:46:55.737Z","dependency_job_id":"5c919f51-386c-44d9-82b9-956f1709a39e","html_url":"https://github.com/gumlet/php-image-resize","commit_stats":{"total_commits":229,"total_committers":47,"mean_commits":4.872340425531915,"dds":0.7729257641921398,"last_synced_commit":"54165d97ebc3d4fc188761166b78e6702f605f59"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gumlet%2Fphp-image-resize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gumlet%2Fphp-image-resize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gumlet%2Fphp-image-resize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gumlet%2Fphp-image-resize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gumlet","download_url":"https://codeload.github.com/gumlet/php-image-resize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253433686,"owners_count":21907777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crop","php","php-library","resize","scale"],"created_at":"2024-08-07T01:01:38.470Z","updated_at":"2025-05-14T02:09:21.831Z","avatar_url":"https://github.com/gumlet.png","language":"PHP","funding_links":[],"categories":["php"],"sub_categories":[],"readme":"php-image-resize\n================\n\nPHP library to resize, scale and crop images.\n\n![Build Status](https://github.com/gumlet/php-image-resize/workflows/PHP%20CI/badge.svg) [![Latest Stable Version](https://poser.pugx.org/gumlet/php-image-resize/v/stable)](https://packagist.org/packages/gumlet/php-image-resize) [![Monthly Downloads](https://poser.pugx.org/gumlet/php-image-resize/d/monthly)](https://packagist.org/packages/gumlet/php-image-resize) [![Coverage Status](https://coveralls.io/repos/github/gumlet/php-image-resize/badge.svg?branch=master)](https://coveralls.io/github/gumlet/php-image-resize?branch=master)\n\nCloud Solution\n---------------\n\nIf you don't want to crop, resize and store images on your server, \u003ca href=\"https://www.gumlet.com\" target=\"_blank\"\u003eGumlet.com\u003c/a\u003e is a **free** service which can process images in real-time and serve worldwide through CDN.\n\n------------------\n\nSetup\n-----\n\nThis package is available through Packagist with the vendor and package identifier the same as this repo.\n\nIf using [Composer](https://getcomposer.org/), in your `composer.json` file add:\n\n```json\n{\n    \"require\": {\n        \"gumlet/php-image-resize\": \"2.0.*\"\n    }\n}\n```\n\nIf you are still using PHP 5.3, please install version ```1.7.0``` and if you are using PHP 5.4, please install version ```1.8.0``` of this library.\n\nWebP support is added with PHP `5.6.0` and current version of library supports that. If you are facing issues, please use `1.9.2` version of this library.\n\nFor PHP versions \u003e= 7.2, `2.0.1` or above version of this library should be used.\n\nOtherwise:\n\n```php\ninclude '/path/to/ImageResize.php';\n```\n\nBecause this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:\n\n```php\n$image = new \\Gumlet\\ImageResize();\n```\n\nOr alias it:\n\n```php\n\nuse \\Gumlet\\ImageResize;\n\n$image = new ImageResize();\n```\n\n\u003e Note: This library uses GD class which do not support resizing animated gif files\n\n------------------\n\nResize\n------\n\nTo scale an image, in this case to half it's size (scaling is percentage based):\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003escale(50);\n$image-\u003esave('image2.jpg');\n```\n\nTo resize an image according to one dimension (keeping aspect ratio):\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003eresizeToHeight(500);\n$image-\u003esave('image2.jpg');\n\n$image = new ImageResize('image.jpg');\n$image-\u003eresizeToWidth(300);\n$image-\u003esave('image2.jpg');\n```\n\nTo resize an image according to a given measure regardingless its orientation (keeping aspect ratio):\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003eresizeToLongSide(500);\n$image-\u003esave('image2.jpg');\n\n$image = new ImageResize('image.jpg');\n$image-\u003eresizeToShortSide(300);\n$image-\u003esave('image2.jpg');\n```\n\nTo resize an image to best fit a given set of dimensions (keeping aspet ratio):\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003eresizeToBestFit(500, 300);\n$image-\u003esave('image2.jpg');\n```\n\nAll resize functions have ```$allow_enlarge``` option which is set to false by default.\nYou can enable by passing ```true``` to any resize function:\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003eresize(500, 300, $allow_enlarge = True);\n$image-\u003esave('image2.jpg');\n```\n\nIf you are happy to handle aspect ratios yourself, you can resize directly:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003eresize(800, 600);\n$image-\u003esave('image2.jpg');\n```\n\nThis will cause your image to skew if you do not use the same width/height ratio as the source image.\n\nCrop\n----\n\nTo to crop an image:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003ecrop(200, 200);\n$image-\u003esave('image2.jpg');\n```\n\nThis will scale the image to as close as it can to the passed dimensions, and then crop and center the rest.\n\nIn the case of the example above, an image of 400px \u0026times; 600px will be resized down to 200px \u0026times; 300px, and then 50px will be taken off the top and bottom, leaving you with 200px \u0026times; 200px.\n\nCrop modes:\n\nFew crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image.\nThe default crop mode used is the `CROPCENTER`.\nAs a result those pieces of code are equivalent:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003ecrop(200, 200);\n$image-\u003esave('image2.jpg');\n```\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003ecrop(200, 200, true, ImageResize::CROPCENTER);\n$image-\u003esave('image2.jpg');\n```\n\nIn the case you have an image of 400px \u0026times; 600px and you want to crop it to 200px \u0026times; 200px the image will be resized down to 200px \u0026times; 300px, then you can indicate how you want to handle those 100px exceeding passing the value of the crop mode you want to use.\n\nFor instance passing the crop mode `CROPTOP` will result as 100px taken off the bottom leaving you with 200px \u0026times; 200px.\n\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003ecrop(200, 200, true, ImageResize::CROPTOP);\n$image-\u003esave('image2.jpg');\n```\n\nOn the contrary passing the crop mode `CROPBOTTOM` will result as 100px taken off the top leaving you with 200px \u0026times; 200px.\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003ecrop(200, 200, true, ImageResize::CROPBOTTOM);\n$image-\u003esave('image2.jpg');\n```\n\nFreecrop:\n\nThere is also a way to define custom crop position.\nYou can define $x and $y in ```freecrop``` method:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003efreecrop(200, 200, $x =  20, $y = 20);\n$image-\u003esave('image2.jpg');\n```\n\nLoading and saving images from string\n-------------------------------------\n\nTo load an image from a string:\n\n```php\n$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));\n$image-\u003escale(50);\n$image-\u003esave('image.jpg');\n```\n\nYou can also return the result as a string:\n\n```php\n$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));\n$image-\u003escale(50);\necho $image-\u003egetImageAsString();\n```\n\nMagic `__toString()` is also supported:\n\n```php\n$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));\n$image-\u003eresize(10, 10);\necho (string)$image;\n```\n\nDisplaying\n----------\n\nAs seen above, you can call `$image-\u003esave('image.jpg');`\n\nTo render the image directly into the browser, you can call `$image-\u003eoutput()`;\n\nImage Types\n-----------\n\nWhen saving to disk or outputting into the browser, the script assumes the same output type as input.\n\nIf you would like to save/output in a different image type, you need to pass a (supported) PHP [`IMAGETYPE_`* constant](http://www.php.net/manual/en/image.constants.php):\n\n- `IMAGETYPE_GIF`\n- `IMAGETYPE_JPEG`\n- `IMAGETYPE_PNG`\n\nThis allows you to save in a different type to the source:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003eresize(800, 600);\n$image-\u003esave('image.png', IMAGETYPE_PNG);\n```\n\nQuality\n-------\n\nThe properties `$quality_jpg`, `$quality_webp` and `$quality_png` are available for you to configure:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003equality_jpg = 100;\n$image-\u003eresize(800, 600);\n$image-\u003esave('image2.jpg');\n```\n\nBy default they are set to 85 and 6 respectively. See the manual entries for [`imagejpeg()`](http://www.php.net/manual/en/function.imagejpeg.php) and [`imagepng()`](http://www.php.net/manual/en/function.imagepng.php) for more info.\n\nYou can also pass the quality directly to the `save()`, `output()` and `getImageAsString()` methods:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003ecrop(200, 200);\n$image-\u003esave('image2.jpg', null, 100);\n\n$image = new ImageResize('image.jpg');\n$image-\u003eresizeToWidth(300);\n$image-\u003eoutput(IMAGETYPE_PNG, 4);\n\n$image = new ImageResize('image.jpg');\n$image-\u003escale(50);\n$result = $image-\u003egetImageAsString(IMAGETYPE_PNG, 4);\n```\n\nWe're passing `null` for the image type in the example above to skip over it and provide the quality. In this case, the image type is assumed to be the same as the input.\n\nInterlacing\n-----------\n\nBy default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned on. It can be disabled by setting `$interlace` to `0`:\n\n```php\n$image = new ImageResize('image.jpg');\n$image-\u003escale(50);\n$image-\u003einterlace = 0;\n$image-\u003esave('image2.jpg');\n```\n\nChaining\n--------\n\nWhen performing operations, the original image is retained, so that you can chain operations without excessive destruction.\n\nThis is useful for creating multiple sizes:\n\n```php\n$image = new ImageResize('image.jpg');\n$image\n    -\u003escale(50)\n    -\u003esave('image2.jpg')\n\n    -\u003eresizeToWidth(300)\n    -\u003esave('image3.jpg')\n\n    -\u003ecrop(100, 100)\n    -\u003esave('image4.jpg')\n;\n```\n\nExceptions\n--------\n\nImageResize throws ImageResizeException for it's own for errors. You can catch that or catch the general \\Exception which it's extending.\n\nIt is not to be expected, but should anything go horribly wrong mid way then notice or warning Errors could be shown from the PHP GD and Image Functions (http://php.net/manual/en/ref.image.php)\n\n```php\ntry{\n    $image = new ImageResize(null);\n    echo \"This line will not be printed\";\n} catch (ImageResizeException $e) {\n    echo \"Something went wrong\" . $e-\u003egetMessage();\n}\n```\n\nFilters\n--------\n\nYou can apply special effects for new image like blur or add banner.\n\n```php\n$image = new ImageResize('image.jpg');\n\n// Add blure\n$image-\u003eaddFilter(function ($imageDesc) {\n    imagefilter($imageDesc, IMG_FILTER_GAUSSIAN_BLUR);\n});\n\n// Add banner on bottom left corner\n$image18Plus = 'banner.png'\n$image-\u003eaddFilter(function ($imageDesc) use ($image18Plus) {\n    $logo = imagecreatefrompng($image18Plus);\n    $logo_width = imagesx($logo);\n    $logo_height = imagesy($logo);\n    $image_width = imagesx($imageDesc);\n    $image_height = imagesy($imageDesc);\n    $image_x = $image_width - $logo_width - 10;\n    $image_y = $image_height - $logo_height - 10;\n    imagecopy($imageDesc, $logo, $image_x, $image_y, 0, 0, $logo_width, $logo_height);\n});\n\n```\n\nFlip\n--------\n\nFlips an image using a given mode and this method is only for PHP version 5.4.\n\n```php\n$flip = new ImageResize('image.png');\n$image = imagecreatetruecolor(200, 100);\n\n$image-\u003eaddFilter(function ($image) {\n    imageflip($image, IMG_FLIP_HORIZONTAL);\n});\n\n```\n\nBoth functions will be used in the order in which they were added.\n\nGamma color correction\n--------\n\nYou can enable the gamma color correction which is disabled by default.\n\n```php\n$image = new ImageResize('image.png');\n$image-\u003egamma(true);\n```\n\nAPI Doc\n-------\n\nhttps://gumlet.github.io/php-image-resize/index.html\n\n------------------\n\nMaintainer\n----------\n\nThis library is maintained by \u003ca href=\"https://www.gumlet.com\" target=\"_blank\"\u003eGumlet.com\u003c/a\u003e\n\n[\u003cimg src=\"https://assets.gumlet.com/public/img/logo.png\" width=\"300px\"\u003e](https://www.gumlet.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgumlet%2Fphp-image-resize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgumlet%2Fphp-image-resize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgumlet%2Fphp-image-resize/lists"}