Image Cropping and Resizing

suggest change

If you have an image and want to create a new image, with new dimensions, you can use imagecopyresampled function:

first create a new image with desired dimensions:

// new image
$dst_img = imagecreatetruecolor($width, $height);

and store the original image into a variable. To do so, you may use one of the createimagefrom* functions where * stands for:

For example:

//original image
$src_img=imagecreatefromstring(file_get_contents($original_image_path));

Now, copy all (or part of) original image (src_img) into the new image (dst_img) by imagecopyresampled:

imagecopyresampled($dst_img, $src_img, 
    $dst_x ,$dst_y, $src_x, $src_y, 
    $dst_width, $dst_height, $src_width, $src_height);

To set src_* and dst_* dimensions, use the below image:

Now, if you want to copy entire of source (initial) image, into entire of destination area (no cropping):

$src_x = $src_y = $dst_x = $dst_y = 0;
$dst_width = $width;// width of new image
$dst_height = $height; //height of new image
$src_width = imagesx($src_img); //width of initial image
$src_height = imagesy($src_img); //height of initial image

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Image processing with GD:
* Image Cropping and Resizing

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
73 Image processing with GD
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR