my way to preload images! haha
Update Coming Soon! Check out Skem9's future roadmap
Search for in

PHP .^. Coding Tutorials | Submited May 24, 2012

Written by: Yohn

Heres a simple function for upload an resizing an image. The function resizes the image into 3 different size, Small, Medium, and Large. You need to have imagemagick enabled on your server for this to work.
I wrote this function a few years ago and have decided to share it with everyone. You need to make sure you have imagemagick installed on your server or this will not work.

What this function is doing it uploading an image to your website/server in a directory of your coosing, creates directies within that directory layed out like the following year/month/day/ and then stores the uploaded image into that /day/ directory in 3 different sizes, Large, Medium, and small.

First the function, save this to uploadNresize.php

Code:
<?php function uploadNresize($file, $directory, $imgName){ $Lwidth = 700; $Lheight = 600; $width = 200; $height = 250; $maxSize = 665600; // KBs in size $mode = 0666; $countinue = true; if(!is_dir($directory)){ if(@mkdir($directory, 0777)){ chmod($directory, 0777); $countinue = true; } else { $countinue = false; } } if(!is_dir($directory.date('Y/'))) { if(@mkdir($directory.date('Y/'), 0777)) { chmod($directory.date('Y/'), 0777); $countinue = true; } else {$countinue = false;} } if(!is_dir($directory.date('Y/m/'))) { if(@mkdir($directory.date('Y/m/'), 0777)) { chmod($directory.date('Y/m/'), 0777); $countinue = true; } else {$countinue = false;} } if(!is_dir($directory.date('Y/m/d/'))) { if(@mkdir($directory.date('Y/m/d/'), 0777)) { chmod($directory.date('Y/m/d/'), 0777);; $countinue = true; } else {$countinue = false;} } if($countinue == true){ if($file[$imgName]['error']) $errors[] = "nothing was uploaded.."; if($file[$imgName]['size'] > $maxSize) $errors[] = "The image size you uploaded was wayyyyyyyy to big for my server to handle sorry."; if($file[$imgName]['type'] != "image/jpeg" && $file[$imgName]['type'] != "image/pjpeg" && $file[$imgName]['type'] != "image/gif" && $file[$imgName]['type'] != "image/png" && $file[$imgName]['type'] != 'image/x-png') $errors[] = "Invalid Image format, supported image types are .jpg (.jpeg), .gif, and .png."; if(empty($errors)){ $origDir = $directory.date('Y/m/d/'); $nr = 0; switch($file[$imgName]['type']){ case "image/jpeg": $ext = '.jpg'; break; case "image/jpg": $ext = '.jpg'; break; case "image/pjpeg": $ext = '.jpg'; break; case "image/gif": $ext = '.gif'; break; case "image/png": $ext = '.png'; break; case "image/x-png": $ext = '.png'; break; } for(;;){ $nr ; if(!file_exists($origDir.'image'.$nr.$ext)) break; } $SfilePath = $origDir."S_image".$nr.$ext; $filePath = $origDir."image".$nr.$ext; $LfilePath = $origDir."L_image".$nr.$ext; $files2 = $file[$imgName]['tmp_name']; move_uploaded_file($files2, $filePath); chmod ($filePath, octdec(666)); $blah = getimagesize($filePath); if($blah[0] > $Lwidth || $blah[1] > $Lheight){ $cmp = "convert ".$filePath." -resize ".$Lwidth."x".$Lheight." ".$LfilePath; $resized = system($cmp); $cmp2 = "convert ".$LfilePath." -resize ".$width."x".$height." ".$filePath; $resized = system($cmp2); $cmp2 = "convert ".$filePath." -resize 100x100 ".$SfilePath; $resized = system($cmp2); } elseif($blah[0] > $width || $blah[1] > $height){ $copy1 = copy($filePath, $LfilePath); $cmp2 = "convert ".$LfilePath." -resize ".$width."x".$height." ".$filePath; $resized = system($cmp2); $cmp2 = "convert ".$filePath." -resize 100x100 ".$SfilePath; $resized = system($cmp2); } else { $copy1 = copy($filePath, $LfilePath); if($blah[0] > 100 || $blah[1] > 100) $resized = system("convert ".$filePath." -resize 100x100 ".$SfilePath); else $resized = copy($filePath, $SfilePath); } $files['small'] = '/'.$SfilePath; $files['medium'] = '/'.$filePath; $files['large'] = '/'.$LfilePath; return $files; } else { $errors[] = "Image error"; } } else { $errors[] = "something happened to the directories permission"; } return $errors; } ?>


And then you have a simple upload form sending the data to to upload.php..

Code:
<form enctype="multipart/form-data" action="upload.php" method="post"><div> Upload File: <input name="image" type="file" /><br /> <input type="submit" value="Upload File" /> </div></form>


and then upload.php would look like the following..

Code:
<?php include('uploadNresize.php'); if(isset($_FILES['image'])){ $send = uploadNresize($_FILES, 'MyDirectory/, 'image'); print_r($send); } ?>


The $send variable is going to be returning an array, either with why the upload failed, or the urls to the image. Usually I do a check like the following to see if it was uploaded correctly before I proceed.
Code:
if(isset($send['small'])){ // the images have been uploaded coreectly, so do whatever here } else { // there was an error uploading the image, foreach($send as $value){ echo '<p>'.$value.'</p>'; } }


If you have imagemagick installed on your server, and cant get this to work find out what the path is to imagemagick. If its /usr/bin you would change = "convert to = "/user/bin/convert


Picture..
meljoy68 ~ 06/25/12, 8:18pm
Thanks for this tutorial!
Picture..
Yohn ~ 06/26/12, 6:57am
glad you like it [smile]