├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── FactoryConvert.php ├── Image.php ├── Load.php ├── MimeType.php ├── Resize.php ├── Save.php ├── factory │ ├── GifResize.php │ ├── ImagesInterfaceResize.php │ ├── JpegResize.php │ └── PngResize.php └── factoryconvert │ ├── ConvertGifToWebp.php │ ├── ConvertJpgToWebp.php │ ├── ConvertPngToWebp.php │ └── FactoryConvertInterface.php └── test ├── ImageResizeTest.php ├── ImageUnitTest.php ├── ay.jpg └── image.html /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diloabininyeri/Image-resize/4ce4d97ef734f2a6fc01aa1ad1503d578ec6ac8d/.gitignore -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '5.4' 4 | - '5.6' 5 | - '7.0' 6 | - '7.1' 7 | - '7.2' 8 | - hhvm # on Trusty only 9 | - nightly 10 | before_script: 11 | - composer install 12 | script: 13 | - ./vendor/bin/phpunit 14 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **php image resize and save library** 2 | 3 | 4 | composer require zeus/image 5 | 6 | ![](https://pilsniak.com/wp-content/uploads/2017/02/logo-composer-transparent.png) 7 | 8 | 9 | 10 | Guide using 11 | 12 | 13 | use Image\Image; 14 | 15 | 16 | class test 17 | { 18 | public function testResize() 19 | { 20 | 21 | 22 | $image = new Image(); 23 | 24 | $image->load((string) "ay.jpg") 25 | ->resize(800, 600) 26 | ->save("sonbir.jpg", 80); 27 | 28 | 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | 36 | (new test())->testResize(); 37 | 38 | 39 | - save method have two parameter save($outputname,$quality) 40 | - load method have one parameter load image load($imagepath) 41 | - resize method have two parameter resize($width,$height) 42 | 43 | **example use on laravel** 44 | 45 | 46 | 47 | [![](https://image.ibb.co/iFnbdy/Screenshot_from_2018_05_17_14_41_00.png)](https://image.ibb.co/iFnbdy/Screenshot_from_2018_05_17_14_41_00.png) 48 | 49 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zeus/image", 3 | "description": "image resize and create thumbnail", 4 | "keywords": ["image", "resize","gd","library","php","thumbnail"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "dılo sürücü", 9 | "email": "berxudar@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php":">7.1", 14 | "phpunit/phpunit": "^7.0" 15 | }, 16 | "autoload":{ 17 | 18 | 19 | "psr-4":{ 20 | 21 | "Image\\":"src" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./test/ 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/FactoryConvert.php: -------------------------------------------------------------------------------- 1 | factory = new ConvertJpgToWebp(); 38 | return $this->factory->convert($file); 39 | 40 | 41 | } else if ($extension == "png" or $extension == "PNG") { 42 | 43 | 44 | $this->factory = new ConvertPngToWebp(); 45 | return $this->factory->convert($file); 46 | 47 | 48 | } else { 49 | 50 | $this->factory = new ConvertJpgToWebp(); 51 | return $this->factory->convert($file); 52 | 53 | 54 | } 55 | 56 | 57 | return $this; 58 | 59 | 60 | } 61 | 62 | 63 | /*** 64 | * @param $filename 65 | * @return mixed 66 | * 67 | * 68 | */ 69 | 70 | public function save($filename,$quality) 71 | { 72 | 73 | 74 | return $this->factory->save($filename,$quality); 75 | 76 | } 77 | } -------------------------------------------------------------------------------- /src/Image.php: -------------------------------------------------------------------------------- 1 | height = $height; 33 | 34 | $this->width = $width; 35 | $this->resize = new Resize(); 36 | $mimetype = new MimeType(); 37 | 38 | $fileMimeType = $mimetype->getMimeTypeOfImage($this->imagePath); 39 | 40 | $this->resize->resize($width, $height, $fileMimeType, $this->imagePath); 41 | 42 | return $this; 43 | } 44 | 45 | /** 46 | * @param $imagePath 47 | * @return $this 48 | * @throws \Exception 49 | * 50 | * 51 | */ 52 | 53 | public function convertWebp() 54 | { 55 | 56 | 57 | $mimetype = new MimeType(); 58 | 59 | $fileMimeType = $mimetype->getMimeTypeOfImage($this->imagePath); 60 | 61 | 62 | $this->convert = new FactoryConvert; 63 | $this->convert->factory($fileMimeType, $this->imagePath); 64 | 65 | 66 | return $this; 67 | 68 | } 69 | 70 | public function load($imagePath) 71 | { 72 | 73 | $this->imagePath = (new Load())->loadImage($imagePath); 74 | 75 | return $this; 76 | } 77 | 78 | /** 79 | * @param $savePath 80 | * @param int $quality 81 | * @return mixed 82 | * 83 | * 84 | * 85 | */ 86 | public function save($savePath, $quality = 100) 87 | { 88 | if ($this->convert) { 89 | 90 | 91 | return $this->convert->save($savePath,$quality); 92 | 93 | } else { 94 | 95 | return $this->resize->save($savePath, $quality); 96 | 97 | } 98 | 99 | 100 | } 101 | } -------------------------------------------------------------------------------- /src/Load.php: -------------------------------------------------------------------------------- 1 | resize = (new JpegResize())->resize($width, $height, $image); 39 | 40 | 41 | } else if ($extension == "png" or $extension == "PNG") { 42 | 43 | 44 | $this->resize = (new PngResize())->resize($width, $height, $image); 45 | 46 | 47 | } else { 48 | 49 | $this->resize = (new GifResize())->resize($width, $height, $image); 50 | 51 | } 52 | 53 | 54 | return $this; 55 | 56 | 57 | } 58 | 59 | /** 60 | * @param $savePath 61 | * @param $quality 62 | * @return mixed 63 | * 64 | * 65 | */ 66 | public function save($savePath,$quality) 67 | { 68 | 69 | 70 | return $this->resize->save($savePath,$quality); 71 | 72 | } 73 | } -------------------------------------------------------------------------------- /src/Save.php: -------------------------------------------------------------------------------- 1 | hedef = imagecreatetruecolor($genislik, $yukseklik); 36 | $kaynak = imagecreatefromgif($dosya); 37 | imagecopyresampled($this->hedef, $kaynak, 0, 0, 0, 0, $genislik, $yukseklik, $mevcutGenislik, $mevcutYukseklik); 38 | 39 | 40 | return $this; 41 | } 42 | 43 | /** 44 | * @param $savepath 45 | * @param $quality 46 | * @return mixed|void 47 | * 48 | * 49 | * 50 | */ 51 | public function save($savepath, $quality) 52 | { 53 | 54 | imagegif($this->hedef, $savepath); 55 | imagedestroy($this->hedef); 56 | 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /src/factory/ImagesInterfaceResize.php: -------------------------------------------------------------------------------- 1 | hedef = imagecreatetruecolor($genislik, $yukseklik); 34 | $kaynak = imagecreatefromjpeg($dosya); 35 | 36 | imagecopyresampled($this->hedef, $kaynak, 0, 0, 0, 0, $genislik, $yukseklik, $mevcutGenislik, $mevcutYukseklik); 37 | 38 | return $this; 39 | } 40 | 41 | /** 42 | * @param $savepath 43 | * @param int $quality 44 | * @return bool 45 | * 46 | * 47 | */ 48 | public function save($savepath, $quality) 49 | { 50 | imagejpeg($this->hedef, $savepath, $quality); 51 | 52 | return imagedestroy($this->hedef); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /src/factory/PngResize.php: -------------------------------------------------------------------------------- 1 | hedef = imagecreatetruecolor($genislik, $yukseklik); 33 | $kaynak = imagecreatefrompng($dosya); 34 | imagecopyresampled($this->hedef, $kaynak, 0, 0, 0, 0, $genislik, $yukseklik, $mevcutGenislik, $mevcutYukseklik); 35 | 36 | 37 | return $this; 38 | } 39 | 40 | /** 41 | * @param $savepath 42 | * @param int $quality 43 | * 44 | * 45 | */ 46 | public function save($savepath, $quality) 47 | { 48 | 49 | imagepng($this->hedef, $savepath, $quality*0.1); 50 | imagedestroy($this->hedef); 51 | 52 | } 53 | } -------------------------------------------------------------------------------- /src/factoryconvert/ConvertGifToWebp.php: -------------------------------------------------------------------------------- 1 | content = imagecreatefromstring($cont); 41 | 42 | 43 | } 44 | 45 | 46 | /** 47 | * @param $filename 48 | * @param $quality 49 | * @return bool|mixed 50 | * 51 | * 52 | */ 53 | public function save($filename,$quality) 54 | { 55 | imagewebp($this->content,$filename); 56 | return imagedestroy($this->content); 57 | 58 | 59 | } 60 | } -------------------------------------------------------------------------------- /src/factoryconvert/ConvertJpgToWebp.php: -------------------------------------------------------------------------------- 1 | content = imagecreatefromstring($cont); 41 | 42 | return $this; 43 | } 44 | 45 | /** 46 | * @param $filename 47 | * @param $quality 48 | * @return bool|mixed 49 | * 50 | * 51 | */ 52 | public function save($filename,$quality) 53 | { 54 | 55 | 56 | imagewebp($this->content,$filename,$quality); 57 | return imagedestroy($this->content); 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /src/factoryconvert/ConvertPngToWebp.php: -------------------------------------------------------------------------------- 1 | content = imagecreatefromstring($cont); 41 | 42 | 43 | return $this; 44 | } 45 | 46 | /** 47 | * @param $filename 48 | * @param $quality 49 | * @return bool|mixed 50 | * 51 | */ 52 | public function save($filename, $quality) 53 | { 54 | 55 | imagewebp($this->content, $filename, $quality); 56 | return imagedestroy($this->content); 57 | } 58 | } -------------------------------------------------------------------------------- /src/factoryconvert/FactoryConvertInterface.php: -------------------------------------------------------------------------------- 1 | load($path) 21 | ->resize(200,150) 22 | ->save(getcwd()."/test/sonbir.jpg",75); 23 | 24 | 25 | $this->assertFileExists(getcwd()."/test/sonbir.jpg",true); 26 | 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /test/ImageUnitTest.php: -------------------------------------------------------------------------------- 1 | load($path) 19 | ->convertWebp() 20 | ->save(getcwd()."/test/sonbir.webp",80); 21 | 22 | $this->assertFileExists(getcwd()."/test/sonbir.webp",true); 23 | 24 | 25 | 26 | 27 | 28 | } 29 | 30 | } 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /test/ay.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diloabininyeri/Image-resize/4ce4d97ef734f2a6fc01aa1ad1503d578ec6ac8d/test/ay.jpg -------------------------------------------------------------------------------- /test/image.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 | 10 | 11 | 12 | --------------------------------------------------------------------------------