

This tutorial demonstrates different ways of resizing the image in PHP. PHP has a built-in library gd to perform image operations we can create functions to resize the image on upload. Remember, when uploading files you need to set the form’s enctype attribute to “multipart/form-data”.Īn upload script handles the image upload and resizing the image if required. In PHP, we can resize the image on upload. Of course we’ll need to create an HTML form with which users can upload photos: Īs soon as the user hits the upload button a POST request will be sent to a script which will handle the file upload process and show the uploaded image to the user for cropping. Now that you’re familiar with the two methods, we’re good to go. The new dimensions are less than or equal to the specified dimensions. For example, a call to thumbnailImage(400, 400, true) on a 1200×768px image will produce a 400×200px version. We can also pass a third argument known as bestfit If this is set true, the image will be resized in such a way that the new dimensions can be contained within the height and width specified. If either the width or height argument to thumbnailImage() is set as 0, the aspect ratio is maintained.
#PHP IMAGE RESIZE AND UPLOAD CODE#
The above code produces a 200×200px version of image. The thumbnailImage() method simply accepts the height and width of the resized image and can be used as follows: thumbnailImage(200, 200)

Finally, writeImage() saves the result back to disk for us. In this case the code will produce a cropped image of size 400×400px starting at 30px from the top and 10px in from the left of the original image. Then we call cropImage() with appropriate arguments. We create an Imagick object first, passing to its constructor the filename of our image. An easy way to resize images in PHP is to use the GD library: original imagecreatefromjpeg ('ORIGINAL.jpg') resized imagecreatetruecolor (NEW WIDTH, NEW HEIGHT) imagecopyresampled (resized, original, 0, 0, 0, 0, NEW WIDTH, NEW HEIGHT, WIDTH, HEIGHT) imagejpeg (resized, 'RESIZED.
#PHP IMAGE RESIZE AND UPLOAD INSTALL#
As a prerequisite, install the PHP’s GD Graphics Library. The first two arguments indicate the height and width of the cropped region, and the last two indicate the X and Y coordinates of the top-left corner of the cropped area. You can compress images in PHP in one of the following ways: Through PHP’s built-in functions, such as imagejpeg (), which takes the path of the output image and quality specification between 1 and 100. The cropImage() method accepts four arguments. Since we’re going to create an image cropper, we’ll mostly be using the two methods: cropImage() and thumbnailimage(). It offers a simple object-oriented interface to use the API you just need to create an instance of the Imagick class and then call the appropriate methods to start manipulating the images. ImageMagick provides a lot of API methods through which you can manipulate an image. The ImageMagick extension performs image processing using the ImageMagick library.
