Home » PHP Image Resize, Crop, Thumbnail, Watermark

PHP Image Resize, Crop, Thumbnail, Watermark

Today, We want to share with you PHP Image Resize, Crop, Thumbnail, Watermark manipulation.In this post we will show you php resize image before move_uploaded_file, hear for Resize , compress and add watermark (optional) to an image in 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.

PHP Image Resize, Crop, Thumbnail, Watermark manipulation

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

Read Also:  Laravel 6 Delete and soft delete Examples

As I will cover this Post with live Working example to develop laravel intervention image upload, so the php image resize on upload for this example is following below.

index.php

<?php
function image_handler($uploaded_img,$destination,$dest_width = 100,$dest_height = 100,$quality = 80,$wmsource = false) {

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

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

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

  if ($dest_width/$dest_height > $src_ratio) {
  $new_h = $dest_width/$src_ratio;
  $new_w = $dest_width;
  } else {
  $new_w = $dest_height*$src_ratio;
  $new_h = $dest_height;
  }
  $x_mid = $new_w/2;
  $y_mid = $new_h/2;
  $newpic = imagecreatetruecolor(round($new_w), round($new_h));
  imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
  $final = imagecreatetruecolor($dest_width, $dest_height);
  imagecopyresampled($final, $newpic, 0, 0, ($x_mid-($dest_width/2)), ($y_mid-($dest_height/2)), $dest_width, $dest_height, $dest_width, $dest_height);

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

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

  $wm_x = $dest_width - $wm_w;
  $wm_y = $dest_height - $wm_h;

  imagecopy($final, $img_wmarks, $wm_x, $wm_y, 0, 0, $dest_width, $dest_height);
  }

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

  return false;
}
?>

<form method="post" enctype="multipart/form-data">
Source Image: <input type="file" name="img_uploaded" />
<input type="submit" value="Handle This Image" />
</form>
<?php

if($_FILES) {

  $uploaded_img = $_FILES['img_uploaded']['tmp_name'];

  $destination = 'img_demo';
  $dest_width = 500;
  $dest_height = 500;
  $quality = 100;

  $wmsource = 'pakainfo.png';

  $success = image_handler($uploaded_img,$destination,$dest_width,$dest_height,$quality,$wmsource);
  if($success) { echo "Your image was saved successfully!"; }
  else { echo "Your image was not saved."; }
}
?>

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Remove Duplicates value From Array in VueJS

Summary

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

I hope you get an idea about PHP Image Resize, Crop, Thumbnail, Watermark manipulation.
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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Resize Image Upload Example
  2. Read Also:  Append PHP HTML code using jQuery AJAX
  3. Read Also:  Image Resize Crop Thumbnail Watermark PHP Script
  4. Read Also:  PHP image manipulation Fits and Fill
  5. Read Also:  cannot be loaded because running scripts is disabled on this system
  6. Read Also:  PHP Automatic Flexify image resizing
  7. Read Also:  Fatal error: Call to undefined function sqlsrv_connect()
  8. Read Also:  JQuery Ajax Dynamic Treeview using PHP MySQL Example
  9. Read Also:  PHP Image Resize Tutorials
  10. Read Also:  PHP Ajax Image Upload Resize Crop using jQuery

Leave a Comment

Your email address will not be published. Required fields are marked *