Image Resize Crop Thumbnail Watermark PHP Script

Today, We want to share with you Image Resize Crop Thumbnail Watermark PHP Script.In this post we will show you php image resize and save to folder, hear for Copy and resize part of an image using PHP we will give you demo and example for implement.In this post, we will learn about Resizing and cropping image with GD while retaining aspect ratio with an example.

Image Resize Crop Thumbnail Watermark PHP Script

There are the Following The simple About Image Resize Crop Thumbnail Watermark PHP Script Full Information With Example and source code.

As I will cover this Post with live Working example to develop php resize image without cropping, so the Resize , compress and add watermark (optional) to an image in php for this example is following below.

Also Read :  Laravel 5.6 Share Variable With Display Data in View

index.php

<?php
function manipulation_img($orignal_src,$destination,$tham_width = 100,$tham_height = 100,$quality = 80,$wp_src = false) {

  $info = getimagesize($orignal_src);
  $imgtype = image_type_to_mime_type($info[2]);


  switch ($imgtype) {
  case 'image/jpeg':
  	$source = imagecreatefromjpeg($orignal_src);
  	break;
  case 'image/gif':
  	$source = imagecreatefromgif($orignal_src);
  	break;
  case 'image/png':
  	$source = imagecreatefrompng($orignal_src);
  	break;
  default:
  	die('Sorry Your File type : Invalid image type.');
  }


  $src_w = imagesx($source);
  $src_h = imagesy($source);
  $src_ratio = $src_w/$src_h;


  if ($tham_width/$tham_height > $src_ratio) {
  $thumb_new_height = $tham_width/$src_ratio;
  $new_w = $tham_width;
  } else {
  $new_w = $tham_height*$src_ratio;
  $thumb_new_height = $tham_height;
  }
  $x_mid = $new_w/2;
  $y_mid = $thumb_new_height/2;


  $new_thumb = imagecreatetruecolor(round($new_w), round($thumb_new_height));
  imagecopyresampled($new_thumb, $source, 0, 0, 0, 0, $new_w, $thumb_new_height, $src_w, $src_h);
  $output = imagecreatetruecolor($tham_width, $tham_height);
  imagecopyresampled($output, $new_thumb, 0, 0, ($x_mid-($tham_width/2)), ($y_mid-($tham_height/2)), $tham_width, $tham_height, $tham_width, $tham_height);

  if($wp_src) {
  $info = getimagesize($wp_src);
  $imgtype = image_type_to_mime_type($info[2]);
  switch ($imgtype) {
  	case 'image/jpeg':
    	$img_water_mark = imagecreatefromjpeg($wp_src);
    	break;
  	case 'image/gif':
    	$img_water_mark = imagecreatefromgif($wp_src);
    	break;
  	case 'image/png':
    	$img_water_mark = imagecreatefrompng($wp_src);
    	break;
  	default:
    	die('Invalid img_water_mark type.');
  }

  $wm_w = imagesx($img_water_mark);
  $wm_h = imagesy($img_water_mark);

  $wm_x = $tham_width - $wm_w;
  $wm_y = $tham_height - $wm_h;

  imagecopy($output, $img_water_mark, $wm_x, $wm_y, 0, 0, $tham_width, $tham_height);
  }

  if(Imagejpeg($output,$destination,$quality)) {
  return true;
  }

  return false;

}
?>


<form method="post" enctype="multipart/form-data">
Orignal Image: <input type="file" name="img_file_uploaded" />
<input type="submit" value="Handle This Image" />
</form>

<?php

if($_FILES) {
  $orignal_src = $_FILES['img_file_uploaded']['tmp_name'];


  $destination = '/www/pakainfo/admin/image/filename.jpg';
  $tham_width = 400;
  $tham_height = 400;
  $quality = 100;


  $wmsource = '/www/pakainfo/admin/img_water_mark/image.png';

  $success = manipulation_img($orignal_src,$destination,$tham_width,$tham_height,$quality,$wmsource);
  if($success) { echo "Good Luck, Your image was saved successfully!"; }
  else { echo "Sorry, Your image was not saved."; }
}

?>

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  PHP image manipulation Fits and Fill

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Image Resize Crop Thumbnail Watermark PHP Script.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.