Added Oct 3, 2018  by Admin

How to upload an image with changing weight and size in PHP?

Most likely, you have already faced the need to not just limit the user in the size of images uploaded to your site. That's right, you can copy the image that the user loads, reduce it, or adjust it to the desired size. Or can you cut out a square image, as they do in social networks? Look at the examples.

How to upload an image with changing weight and size in PHP?

In this note, I want to show a very simple function that resalizes the image more or less correctly in accordance with the proportions of the original and the new dimensions we need. The need to change sizes often occurs for example in the manufacture of avatar or when we need to preview images for some galleries.

In the following examples, only image PROCESSING, not the entire form of download) About the download can be found here.

Loading and compressing images in PHP

In the first example, we determine the maximum size of the new image, create a new one in proportion to the smaller size, delete the source.

<?php 
// SAMPLE FILE. 
$filename = 'test.jpg'; 

// CHECK FOR THE IMAGE
$size = also($filename); // if this image and it sized, then continue
if ($size){
	// DEFINE THE MAXIMUM WIDTH AND HEIGHT OF THE IMAGE
	$width = 200; 
	$height = 200; 

	// WORKING CORRECTLY. DEFINES THE TYPE OF
	header ("Content-type: {$size ['mime')]}");

	// GET THE NEW DIMENSIONS
	list ($width_orig, $height_orig) = getimagesize($filename); 

	if ($width & & ($width_orig < $height_orig)) { 
		$width = ($height / $height_orig) * $width_orig; 
	} else { 
		$height = ($width / $width_orig) * $height_orig; 
	} 

	//SAVE THE IMAGE
	$image_p = imagecreatetruecolor($width, $height); 
	$image = imagecreatefromjpeg($filename); 
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 

	// CREATE
	imagejpeg ($image_p, null, 100); 

	// DESTROY THE SOURCE CODE - NEED AN ADDRESS. For EXAMPLE, images/img001.jpg
	unlink ($filename);

} else {
	exit ("the Downloaded file is not an image...");
}
?>

Loading and cutting part of the image in PHP

In this example, load the original image, cut the square (regardless of the size of the source), copy the cut part to the new image, delete the source. It's simple :-)

$upload_photo = $_FILES['upload_photo']['name']; // file to be downloaded from the form in the upload_photo field
$path_to_90_directory = 'images/'; // folder where the initial image and its compressed copy will be loaded
    
if(preg_match ('/[.](JPG)|(jpg)|(gif)|(GIF)|(png)/(PNG)$/',$_FILES['upload_photo'] ['name']){ // check the format of the original image
        $filename = $_FILES ['upload_photo']['name'];
        $source = $_FILES ['upload_photo']['tmp_name'];    
        $target = $path_to_90_directory.$filename;
        move_uploaded_file($source, $target); // upload the original to the $path_to_90_directory folder

    if(preg_match ('/[.](GIF)/(gif)$/', $filename)) {
		$im = imagecreatefromgif ($path_to_90_directory.$filename); //if the original was in gif format, then create an image in the same format. Necessary for subsequent compression
    }
    if(preg_match ('/[.](PNG)/(png)$/', $filename)) {
		$im = imagecreatefrompng ($path_to_90_directory.$filename); //if the original was in png format, then create an image in the same format. Necessary for subsequent compression
    }
    
    if(preg_match ('/[.](JPG)|(jpg)|(jpeg)/(JPEG)$/', $filename)) {
        $im = imagecreatefromjpeg ($path_to_90_directory.$filename); //if the original was in jpg format, then create an image in the same format. Necessary for subsequent compression
    }

$w = 200; // square 90x90. Can be put and the other the size of the.

// create original image based on 
// source file and determine its size 
$w_src = imagesx ($im); // define width
$h_src = imagesy ($im); // define image height

         // create an empty square image 
         // truecolor is important!, otherwise we will have an 8-bit result 
         $dest = imagecreatetruecolor($w,$w); 

         // cut out the square middle x, if the photo is horizontal 
         if ($w_src>$h_src) 
			imagecopyresampled($dest, $im, 0, 0,
                          round ((max ($w_src,$h_src) - min ($w_src,$h_src)) / 2),
                          0, $w, $w, min($w_src,$h_src), min($w_src,$h_src)); 

         // cut square on the top of y, 
         // if photo vertical (though can be, too, the middle of the) 
         if ($w_src<$h_src) 
			imagecopyresampled($dest, $im, 0, 0, 0, 0, $w, $w,
                          min ($w_src,$h_src), min($w_src,$h_src)); 

         // square image scales without clippings 
         if ($w_src==$h_src) 
			imagecopyresampled($dest, $im, 0, 0, 0, 0, $w, $w, $w_src, $w_src); 
         

$date=time (); //calculate the current time.

imagejpeg ($dest, $path_to_90_directory.$date.".jpg"); // save the jpg image to the desired folder, the name will be the current time. 

//why jpg? It takes up very little space + destroyed gif image animation that distracts the user. 

$avatar = $path_to_90_directory.$date.".jpg"; //variable in the path to the avatar.

$delfull = $path_to_90_directory.$filename; // get source address
unlink ($delfull); //delete the original uploaded image, we no longer need it. The task was to get a miniature.

(0 ratings, average: 0 out of 5)