We all have been in situation where we allow users to upload images for content items or profile pictures. They’ll just upload huge images without considering bandwidth and disk space. Soon you have hundreds images above 2MB in size cluttering your precious harddisk space and taking up all of the bandwidth with just a few pageviews.
So how do you conquer this problem? Well, resize the images automatically. Keeping their ratio and quality but reducing their size significantly.
Here is some script that is made for resizing pictures with php help:
<?php |
if isset($_POST[‘Submit’])) |
{ |
$size = 150; // the thumbnail height |
$filedir = ‘pics/’; // the directory for the original image |
$thumbdir = ‘pics/’; // the directory for the thumbnail image |
$prefix = ‘small_’; // the prefix to be added to the original name |
$maxfile = ‘2000000’; |
$mode = ‘0666’; |
$userfile_name = $_FILES[‘image’][‘name’]; |
$userfile_tmp = $_FILES[‘image’][‘tmp_name’]; |
$userfile_size = $_FILES[‘image’][‘size’]; |
$userfile_type = $_FILES[‘image’][‘type’]; |
if (isset($_FILES[‘image’][‘name’])) |
{ |
$prod_img = $filedir.$userfile_name; |
$prod_img_thumb = $thumbdir.$prefix.$userfile_name; |
move_uploaded_file($userfile_tmp, $prod_img); |
chmod ($prod_img, octdec($mode)); |
$sizes = getimagesize($prod_img); |
$aspect_ratio = $sizes[1]/$sizes[0]; |
if ($sizes[1] <= $size) |
{ |
$new_width = $sizes[0]; |
$new_height = $sizes[1]; |
}else{ |
$new_height = $size; |
$new_width = abs($new_height/$aspect_ratio); |
} |
$destimg=ImageCreateTrueColor($new_width,$new_height) |
or die(‘Problem In Creating image’); |
$srcimg=ImageCreateFromJPEG($prod_img) |
or die(‘Problem In opening Source Image’); |
if(function_exists(‘imagecopyresampled’)) |
{ |
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) |
or die(‘Problem In resizing’); |
}else{ |
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) |
or die(‘Problem In resizing’); |
} |
ImageJPEG($destimg,$prod_img_thumb,90) |
or die(‘Problem In saving’); |
imagedestroy($destimg); |
} |
echo ‘ |
<a href=”‘.$prod_img.‘”> |
<img src=”‘.$prod_img_thumb.‘” width=”‘.$new_width.‘” heigt=”‘.$new_height.‘”> |
</a>’; |
}else{ |
echo ‘ |
<form method=”POST” action=”‘.$_SERVER[‘PHP_SELF’].‘” enctype=”multipart/form-data”> |
<input type=”file” name=”image”><p> |
<input type=”Submit” name=”Submit” value=”Submit”> |
</form>’; |
} |
?> |
If you are a begginer as I am, you will find this helpfull. I also made a ZIP archive that contains the  files you need in working with this script. Just download, unzip, upload to your server and play 😀
Download from here pictures.zip
10 comments
Skip to comment form
whats up great blog and theme. I really hope I am not troubling you I just sought to ask just what wordpress plugin you utilize to show the newest remarks on your blog? I want to do something similar for my blog yet I can’t find the plugin or widget for it. Thanks for your time 🙂
Machine translation failed. retry
Well I sincerely enjoyed reading it. This tip provided by you is very constructive for good planning.
Machine translation failed. retry
[…]Pingback[…]
Machine translation failed. retry
I signed up in your publication, so please keep up the informative posts, Perhaps there is a way to disable that online system;1$4
Machine translation failed. retry
wonderful points altogether, you just gained a new reader. What would you suggest in regards to your post that you made some days ago? Any positive?
Machine translation failed. retry