├── .github └── ISSUE_TEMPLATE │ ├── bug-report.md │ ├── feature_request.md │ └── help-needed.md ├── .gitignore ├── .travis.yml ├── changelog.md ├── composer.json ├── contributing.md ├── example.php ├── license ├── phpunit.xml ├── readme.md ├── src └── ImageConverter.php └── tests ├── ImageConverterTest.php └── fixtures └── from ├── dices.png └── road.jpg /.github/ISSUE_TEMPLATE/bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **PHP version:** 11 | Add phpinfo() to see your PHP version. 12 | 13 | **Error message:** 14 | >If you get an error message, paste it here. 15 | 16 | **Describe the bug** 17 | A clear and concise description of what the bug is. 18 | 19 | **To Reproduce** 20 | 1. 21 | 2. 22 | 3. 23 | 24 | **Expected behavior** 25 | A clear and concise description of what you expected to happen. 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | Make sure that your idea has not already been added as an issue by someone else. 11 | 12 | **Describe your idea** 13 | A clear and concise description of what you want to happen. 14 | 15 | **Describe alternatives you've considered** 16 | A clear and concise description of any alternative solutions or features you've considered. 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/help-needed.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Help needed 3 | about: In case you need help we will try out our best to help you. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **PHP version:** 11 | Use phpinfo() to see your PHP version. 12 | 13 | **Explain your problem:** 14 | Explain the problem as detailed as possible. 15 | 16 | *^Steps to reproduce:** 17 | 1. 18 | 2. 19 | 3. 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | tests/fixtures/to/* 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.1 5 | - 7.2 6 | - 7.3 7 | 8 | before_script: 9 | - composer install 10 | 11 | script: 12 | - mkdir -p build/logs 13 | - ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml 14 | 15 | after_success: 16 | - travis_retry php vendor/bin/php-coveralls -v 17 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.2 beta 4 | 5 | - Added exceptions for quality out of range 6 | - Removed quality argument from gif conversion 7 | 8 | ## 1.1 9 | 10 | - Changed namespace from `Jens\ImageConverter` to `ImageConverter` 11 | - Added `convert` helper function 12 | - Changed `example.php` to include the shorter version 13 | 14 | ## 1.0 15 | 16 | - Initial release -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jens/php-image-converter", 3 | "description": "PHP class to convert image formats", 4 | "keywords": ["php", "image", "conversion"], 5 | "type": "library", 6 | "homepage": "https://github.com/jenstornell/php-image-converter", 7 | "license": "MIT", 8 | "authors": [{ 9 | "name": "Jens Törnell", 10 | "homepage": "http://www.jenst.se/" 11 | }], 12 | "require": { 13 | "php": "^7.1", 14 | "ext-gd": "*", 15 | "ext-exif": "*" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Jens\\ImageConverter\\": "src/" 20 | } 21 | }, 22 | "autoload-dev": { 23 | "psr-4": { 24 | "Jens\\ImageConverter\\Tests\\": "test/" 25 | } 26 | }, 27 | "require-dev": { 28 | "phpunit/phpunit": "^7.0", 29 | "php-coveralls/php-coveralls": "^2.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | tests 7 | 8 | 9 | 10 | 11 | src 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # PHP Image Converter 2 | 3 | *Version 1.2 beta* 4 | 5 | PHP library to convert between **gif**, **jpeg**, **png** and **webp** image files. 6 | 7 | ## Usage 8 | 9 | ### Syntax 10 | 11 | ```php 12 | echo \ImageConverter\convert(string $from , string $to , int $quality); 13 | ``` 14 | 15 | ### Example 16 | 17 | ```php 18 | 'gif', 19 | IMAGETYPE_JPEG => 'jpeg', 20 | IMAGETYPE_PNG => 'png', 21 | IMAGETYPE_WEBP => 'webp', 22 | ]; 23 | 24 | /** 25 | * Do image conversion work 26 | * 27 | * @param string $from 28 | * @param string $to 29 | * 30 | * @return resource 31 | * @throws \InvalidArgumentException 32 | */ 33 | public function convert($from, $to, $quality = null) 34 | { 35 | $image = $this->loadImage($from); 36 | if (!$image) { 37 | throw new \InvalidArgumentException(sprintf('Cannot load image from %s', $from)); 38 | } 39 | 40 | return $this->saveImage($to, $image, $quality); 41 | } 42 | 43 | private function loadImage($from) 44 | { 45 | $extension = $this->getRealExtension($from); 46 | 47 | if (!array_key_exists($extension, $this->constImageFormat)) { 48 | throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension)); 49 | } 50 | 51 | $format = $this->constImageFormat[$extension]; 52 | 53 | switch ($format) { 54 | case 'gif': 55 | $image = imagecreatefromgif($from); 56 | break; 57 | case 'jpg': 58 | case 'jpeg': 59 | $image = imagecreatefromjpeg($from); 60 | break; 61 | case 'png': 62 | $image = imagecreatefrompng($from); 63 | break; 64 | case 'webp': 65 | $image = imagecreatefromwebp($from); 66 | break; 67 | default: 68 | $image = null; 69 | } 70 | return $image; 71 | } 72 | 73 | private function saveImage($to, $image, $quality) 74 | { 75 | $extension = $this->getExtension($to); 76 | 77 | if ($extension === 'jpg') { 78 | $extension = 'jpeg'; 79 | } 80 | 81 | if (!in_array($extension, $this->imageFormat)) { 82 | throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension)); 83 | } 84 | if (!file_exists(dirname($to))) { 85 | $this->makeDirectory($to); 86 | } 87 | 88 | 89 | if(isset($quality) && !is_int($quality)) { 90 | throw new \InvalidArgumentException(sprintf('The %s quality has to be an integer', $quality)); 91 | } 92 | 93 | switch ($extension) { 94 | case 'gif': 95 | $image = imagegif($image, $to); 96 | break; 97 | case 'jpg': 98 | case 'jpeg': 99 | if ($quality < -1 && $quality > 100) { 100 | throw new \InvalidArgumentException(sprintf('The %s quality is out of range', $quality)); 101 | } 102 | $image = imagejpeg($image, $to, $quality); 103 | break; 104 | case 'png': 105 | if ($quality < -1 && $quality > 9) { 106 | throw new \InvalidArgumentException(sprintf('The %s quality is out of range', $quality)); 107 | } 108 | $image = imagepng($image, $to, $quality); 109 | break; 110 | case 'webp': 111 | if ($quality < 0 || $quality > 100) { 112 | throw new \InvalidArgumentException(sprintf('The %s quality is out of range', $quality)); 113 | } 114 | $image = imagewebp($image, $to, $quality); 115 | break; 116 | default: 117 | $image = null; 118 | } 119 | 120 | return $image; 121 | } 122 | 123 | /** 124 | * Given specific $path to detect current image extension 125 | */ 126 | private function getRealExtension($path) 127 | { 128 | $extension = exif_imagetype($path); 129 | 130 | if (!array_key_exists($extension, $this->constImageFormat)) { 131 | throw new \InvalidArgumentException(sprintf('Cannot detect %s extension', $path)); 132 | } 133 | 134 | return $extension; 135 | } 136 | 137 | /** 138 | * Get image extension from specific $path 139 | * 140 | * @param string $path 141 | * 142 | * @return string 143 | */ 144 | private function getExtension($path) 145 | { 146 | $pathInfo = pathinfo($path); 147 | 148 | if (!array_key_exists('extension', $pathInfo)) { 149 | throw new \InvalidArgumentException(sprintf('Cannot find extension from %s', $path)); 150 | } 151 | 152 | return $pathInfo['extension']; 153 | } 154 | 155 | /** 156 | * Try creating the directory 157 | * 158 | * @return bool 159 | * @throws \InvalidArgumentException 160 | */ 161 | private function makeDirectory($to) 162 | { 163 | $result = @mkdir(dirname($to), 0755); 164 | 165 | if (!$result) { 166 | throw new \InvalidArgumentException(\sprintf('Cannot create %s directory', $to)); 167 | } 168 | 169 | return $result; 170 | } 171 | } 172 | 173 | /** 174 | * Helper function 175 | * 176 | * @param string $from 177 | * @param string $to 178 | * 179 | * @return resource 180 | * @throws \InvalidArgumentException 181 | */ 182 | function convert($from, $to, $quality = null) { 183 | $converter = new ImageConverter(); 184 | return $converter->convert($from, $to, $quality); 185 | } -------------------------------------------------------------------------------- /tests/ImageConverterTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($converter->convert($from, $to, 5)); 35 | } 36 | 37 | /** 38 | * @dataProvider jpgToAvailablImagesProvider 39 | */ 40 | public function testJpgToAvailablImages($to) 41 | { 42 | $from = __DIR__ . '/fixtures/from/road.jpg'; 43 | $converter = new ImageConverter(); 44 | 45 | $this->assertTrue($converter->convert($from, $to, 5)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/fixtures/from/dices.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/free-open-source/php-image-converter/e940f840015932bf4cf9744da50d5bfe8602a493/tests/fixtures/from/dices.png -------------------------------------------------------------------------------- /tests/fixtures/from/road.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/free-open-source/php-image-converter/e940f840015932bf4cf9744da50d5bfe8602a493/tests/fixtures/from/road.jpg --------------------------------------------------------------------------------