Crop Image

Displays a selected chunk of an image. In the example provided, the upper left 100px x 100px are shown.


$filename= "test.jpg";
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefromjpeg($filename);

$src_x = '0';   // begin x
$src_y = '0';   // begin y
$src_w = '100'; // width
$src_h = '100'; // height
$dst_x = '0';   // destination x
$dst_y = '0';   // destination y

$dst_im = imagecreatetruecolor($src_w, $src_h);
$white = imagecolorallocate($dst_im, 255, 255, 255);
imagefill($dst_im, 0, 0, $white);

imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

header("Content-type: image/png");



  1. User Avatar
    Michael Calkins
    Permalink to comment#

    I added your script to another php class I found that lets you resize and save the image. Thanks for the added functionality ;)


  2. User Avatar
    Permalink to comment#

    Can anyone describe, that how this works

  3. User Avatar

    How to get the screenshot of webpage by php?

  4. User Avatar
    Riken Shah
    Permalink to comment#

    Thanks for sharing the information like this. It is very Userful.

  5. User Avatar
    Gopal Sharma

    I am trying to hide Source Code from the user, such that no one can copy my CSS, JS, JQuery and Content(for Content i disabled Keyboard and right click, but it is not enough, as user can go in view-source). I know it is impossible but tell me the trick though which no one can copy my CSS, JS. I am built my Pages in PHP, HTML, CSS, JS, JQuery. So give me solution in any of these language.
    Thank You,

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment