{"id":15589552,"url":"https://github.com/sokil/php-image","last_synced_at":"2025-04-28T11:16:16.639Z","repository":{"id":20809090,"uuid":"24094589","full_name":"sokil/php-image","owner":"sokil","description":"Image manipulation library (resize, filtering, combine, watermarks)","archived":false,"fork":false,"pushed_at":"2020-09-12T21:23:49.000Z","size":1879,"stargazers_count":5,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-28T11:16:05.023Z","etag":null,"topics":["image","image-processing","php"],"latest_commit_sha":null,"homepage":"","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/sokil.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-09-16T10:05:23.000Z","updated_at":"2020-09-12T21:20:29.000Z","dependencies_parsed_at":"2022-08-30T11:32:24.878Z","dependency_job_id":null,"html_url":"https://github.com/sokil/php-image","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sokil%2Fphp-image","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sokil%2Fphp-image/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sokil%2Fphp-image/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sokil%2Fphp-image/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sokil","download_url":"https://codeload.github.com/sokil/php-image/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251302782,"owners_count":21567601,"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":["image","image-processing","php"],"created_at":"2024-10-02T23:02:34.665Z","updated_at":"2025-04-28T11:16:16.580Z","avatar_url":"https://github.com/sokil.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"php-image\n=========\n\n[![Build Status](https://travis-ci.org/sokil/php-image.png?branch=master\u00261)](https://travis-ci.org/sokil/php-mongo)\n[![Latest Stable Version](https://poser.pugx.org/sokil/php-image/v/stable.png)](https://packagist.org/packages/sokil/php-image)\n[![Coverage Status](https://coveralls.io/repos/sokil/php-image/badge.png?branch=master)](https://coveralls.io/r/sokil/php-image?branch=master)\n[![Gitter](https://badges.gitter.im/JoinChat.svg)](https://gitter.im/sokil/php-image?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n* [Installation](#installation)\n* [Open image](#open-image)\n* [Resize image](#resize-image)\n* [Crop image](#crop-image)\n* [Rotate image](#rotate-image)\n* [Flip image](#flip-image)\n* [Filters](#filters)\n* [Image elements](#image-elements)\n  * [Adding elements to image](#adding-elements-to-image)\n  * [Writing text](#writing-text)\n* [Save image](#save-image)\n\nInstallation\n------------\n\nYou may install library through composer:\n```\ncomposer require sokil/php-image\n```\n\nOpen image\n----------\n\nCreate instance of image:\n```php\n$image = new \\Sokil\\Image\\Image($pathToImage);\n```\n\nFactory incapsulates instantiating of all image objects and allow to confirure created images:\n```php\n$factory = new \\Sokil\\Image\\Factory;\n```\n\nOpening from filename:\n\n```php\n$factory-\u003eopenImage('/path/to/image.jpeg');\n```\n\nOpening from GD resource:\n\n```php\n$factory-\u003eopenImage($imageResource);\n```\n\nCreating new image:\n```\n$image = $factory-\u003ecreateImage(300, 200);\n```\n\nResize image\n------------\n\nThere is four resize modes: 'scale', 'fit', 'crop' and 'cache'.\n\n```php\n$newImage = $factory-\u003eresizeImage($image, $mode, $width, $height);\n```\n\nIf you want to register own resize strategy, extend class from \\Sokil\\Image\\AbstractResizeStrategy and add namespase:\n```php\n// through factory constructor\n$factory = new \\Sokil\\Image\\Factory([\n    'namespace' =\u003e [\n        'resize' =\u003e '\\Vendor\\ResizeStrategy',\n    ],\n]);\n// through factory method\n$factory-\u003eaddResizeStrategyNamespace('\\Vendor\\ResizeStrategy');\n// directly to image\n$image-\u003eaddResizeStrategyNamespace('\\Vendor\\ResizeStrategy');\n```\nClasses searches in priority of adding.\n\nCrop image\n----------\n\nTo get part of image by specified wifth and height and in defined coordinates use:\n```php\n$x = 10;\n$y = 10;\n$width = 20;\n$height = 20;\n\n$image-\u003ecrop($x, $y, $width, $height);\n```\n\nRotate image\n------------\n\nRotating is counter clockwise;\n\nRotate on 90 degrees:\n```php\n$image-\u003erotate(90);\n```\n\nRotate on 45 degrees, and fill empty field with black color:\n```php\n$image-\u003erotate(45, '#000000');\n```\n\nRotate on 45 degrees, and fill empty field with transparent green color:\n```php\n$image-\u003erotate(45, '#8000FF00');\n```\n\nFlip image\n----------\n\nFlip in vertical direction:\n```php\n$image-\u003eflipVertical();\n```\n\nFlip in horisontal direction\n```php\n$image-\u003eflipHorisontal();\n```\n\nFlip in both directions\n```php\n$image-\u003eflipBoth();\n```\n\nFilters\n-------\n\nGreyscale image:\n```php\n$factory-\u003efilterImage($image, 'greyscale');\n```\n\nIf you want to register own filter strategy to support new filters, extend class from \\Sokil\\Image\\AbstractFilterStrategy and add namespase:\n```php\n// through factory constructor\n$factory = new \\Sokil\\Image\\Factory([\n    'namespace' =\u003e [\n        'filter' =\u003e '\\Vendor\\FilterStrategy',\n    ],\n]);\n// through factory method\n$factory-\u003eaddFilterStrategyNamespace('\\Vendor\\FilterStrategy');\n// or directly to image\n$image-\u003eaddFilterStrategyNamespace('\\Vendor\\FilterStrategy');\n```\nClasses searches in priority of adding.\n\nImage elements\n--------------\n\n### Adding elements to image\n\nElement is everything that can me append to image: text, shape, other image. First we need to create element instabce and configure it:\n```php\n$someElement = $factory-\u003ecreateElement('someElement')-\u003esetParam1('someValue');\n```\n\nThan element placed to image to some coordinates:\n```php\n$image-\u003eappendElementAtPosition($someElement, 30, 30);\n```\n\nYou can create your own elements that inherits \\Sokil\\Image\\AbstractElement class, and register namespace:\n```php\nnamespace Vendor\\Elements;\n\nclass Circle extends \\Sokil\\Image\\AbstractElement\n{\n    public function setRadius($r) { // code to set radius }\n    \n    public function draw($resource, $x, $y) \n    {\n        // code to draw circle on image $resouce at coordinates ($x, $y)\n    }\n}\n\n// through factory constructor\n$factory = new \\Sokil\\Image\\Factory([\n    'namespace' =\u003e [\n        'element' =\u003e '\\Vendor\\Element',\n    ],\n]);\n// through factory method\n$factory-\u003eaddElementNamespace('\\Vendor\\Elements');\n```\n\nNow you can draw your own circles:\n```php\n$circle = $factory-\u003ecreateElement('circle')-\u003esetRadiud(100);\n$image-\u003eappendElementAtPosition($circle, 100, 100);\n```\n\n### Writing text\n\nTo create text element you can use one of methods: \n```php\n$textElement = $factory-\u003ecreateElement('text');\n// or through helper \n$textElement = $factory-\u003ecreateTextElement();\n```\n\nFirst we need to configure text element:\n```php\n$factory = new \\Sokil\\Image\\Factory();\n        \n// text element\n$element = $factory\n    -\u003ecreateTextElement()\n    -\u003esetText('hello world')\n    -\u003esetAngle(20)\n    -\u003esetSize(40)\n    -\u003esetColor('#ababab')\n    -\u003esetFont(__DIR__ . '/FreeSerif.ttf');\n```\n\nNow we need to place element in image at some coordinates:\n```php\n$image-\u003eappendElementAtPosition($element, 50, 150);\n```\n\nSave image\n----------\n\nLibrary supports three formats of image: 'jpeg', 'png' and 'gif'. \n\nTo write image to disk you must define format of image and configure write strategy:\n```php\n$factory-\u003ewriteImage($image, 'jpeg', function(\\Sokil\\Image\\WriteStrategy\\JpegWriteStrategy $strategy) {\n    $strategy-\u003esetQuality(98)-\u003etoFile('/path/to/file.jpg');\n});\n```\n\nTo send image to STDOUT you must define format of image and configure write strategy:\n```php\n$factory-\u003ewriteImage($image, 'jpeg', function(\\Sokil\\Image\\WriteStrategy\\JpegWriteStrategy $strategy) {\n    $strategy-\u003esetQuality(98)-\u003etoStdout();\n});\n```\n\nIf you want to register own write strategy to support new image format, extend class from \\Sokil\\Image\\AbstractWriteStrategy and add namespase:\n```php\n// through factory constructor\n$factory = new \\Sokil\\Image\\Factory([\n    'namespace' =\u003e [\n        'write' =\u003e '\\Vendor\\WriteStrategy',\n    ],\n]);\n// through factory method\n$factory-\u003eaddWriteStrategyNamespace('\\Vendor\\WriteStrategy');\n// or directly to image\n$image-\u003eaddWriteStrategyNamespace('\\Vendor\\WriteStrategy');\n```\nClasses searches in priority of adding.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsokil%2Fphp-image","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsokil%2Fphp-image","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsokil%2Fphp-image/lists"}