How to create thumbnail by resizing image in PHP using dyreimage-php

DYReImage-PHP

← Prev

In this tutorial we will learn to create thumbnail by resizing image in PHP using dyreimage-php.

Lets say, we have the following project structure.

And lets say, we want to create a thumbnail by resizing the sample.jpeg file which is inside the image folder.

So, we will open the index.php file and we will write the following code.

<?php

require_once 'DYReImage/autoload.php';

$source = __DIR__ . '/image/sample.jpeg';
$destination = __DIR__ . '/image/output.png';
$option = array(
		"height" => 200,
		"width" => "auto",
		"quality" => 80
);

try {
	
	$obj = new DYReImage\DYReImage($source, $destination, $option);
	$obj->resize();
	
	echo "Done!";
	
} catch(\Exception $e) {
	die("Error: " . $e->getMessage());
}
?>

If we run the index.php code at this moment we may get PHP WARNING and Error Failed to save image to file. This will occur because we don't have the write permission for the image directory.

PHP Warning:  imagejpeg(/Users/yusufshakeel/Sites/myproject/image/output.png): failed to open stream: Permission denied in /Users/yusufshakeel/Sites/myproject/DYReImage/DYReImage.php on line 288

To solve this issue we can set the chmod of the image directory to 777.

YUSUF-MacBook-Pro:myproject yusufshakeel$ ls -la
total 24
drwxr-xr-x   6 yusufshakeel  staff   204 Apr 22 16:48 .
drwxr-xr-x  32 yusufshakeel  staff  1088 Apr 22 16:47 ..
drwxr-xr-x@  5 yusufshakeel  staff   170 Apr 22 15:33 DYReImage
drwxr-xr-x@  3 yusufshakeel  staff   102 Apr 22 15:33 image
-rw-r--r--@  1 yusufshakeel  staff   704 Apr 22 17:30 index.php

YUSUF-MacBook-Pro:myproject yusufshakeel$ chmod 777 image/
YUSUF-MacBook-Pro:myproject yusufshakeel$ ls -la
total 24
drwxr-xr-x   6 yusufshakeel  staff   204 Apr 22 16:48 .
drwxr-xr-x  32 yusufshakeel  staff  1088 Apr 22 16:47 ..
drwxr-xr-x@  5 yusufshakeel  staff   170 Apr 22 15:33 DYReImage
drwxrwxrwx@  3 yusufshakeel  staff   102 Apr 22 15:33 image
-rw-r--r--@  1 yusufshakeel  staff   704 Apr 22 17:30 index.php
YUSUF-MacBook-Pro:myproject yusufshakeel$

And now if we run the code we will get the resized image.

← Prev