|
|
|
|
|
|
| |
Hello all! I wrote this example in hopes to create a simple way to upload an image as well as create a thumbnail image. All this script basically does is uploads the image to a temporary location, copies it to it's permanent location, then creates a smaller thumbnail image and saves it to the thumbnail image location. On a side note, this script is set up only to allow jpg images, though that ca be changed. Feel free to modify it if you need to in order to integrate it into your site. Some practical uses for it would be as a photo upload for the webmaster or just for users in general on a site (you could include a MySQL query to add info to a database to make displaying photos easier later on). For a user upload, just add a little PHP blurb that only displays it if the user is logged in. Hope this helps some!
|
<?php
$idir = "images/"; // Path To Images Directory
$tdir = "images/thumbs/"; // Path To Thumbnails Directory
$twidth = "125"; // Maximum Width For Thumbnail Images
$theight = "100"; // Maximum Height For Thumbnail Images
if (!isset($_GET['subpage'])) { // Image Upload Form Below ?>
<form method="post" action="addphoto.php?subpage=upload" enctype="multipart/form-data">
File:<br />
<input type="file" name="imagefile" class="form">
<br /><br />
<input name="submit" type="submit" value="Sumbit" class="form"> <input type="reset" value="Clear" class="form">
</form>
<? } else if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
$url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print 'Image thumbnail created successfully.'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
}
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
}
} ?> | | |
|
| Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality. Categories : PHP, GD image library, Graphics | | | Create a Thumbnail Using PHP, GD and Exif Categories : PHP, Graphics, Exif, GD image library | | | PHP Email image generator - hide your email from bots - using the GD Library Categories : PHP, Graphics, GD image library, Beginner Guides | | | Advanced Image WaterMarker Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented | | | Line graphics generation library written in PHP + GD library (spanish comments) Categories : PHP, Graphics, GD image library | | | A captcha image allows you to prevent spam posting when users reload the page and stop bots from submitting forms automatically. This version allows you to use your own fonts (.ttf) to show the text.
Categories : PHP, Security, Graphics, GD image library | | | Simple PHP Bar Graph using GD library Categories : PHP, GD image library, Graphics, Arrays | | | Image Generation Class ( PNG Format ) Categories : PHP, GD image library, PHP Classes, Graphics | | | EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly. Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library | | | PHP Image Compression using GD library Categories : PHP, Compression, GD image library, Graphics | | | a simple pie-chart in php3 (with gd) Categories : PHP, Graphics, GD image library, Charts and Graphs | | | Display a bar chart based on random values. Categories : Graphics, PHP, GD image library, Charts and Graphs | | | CAPTCHA[Image verification] Categories : PHP, Security, GD image library, Graphics, Sessions | | | imageMarker v 3.00 with new advanced features Categories : PHP, PHP Classes, Graphics, GD image library | | | Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs | |
| | | | mike ar wrote :1899
Hey nick!this script is super practicle!one question how would i be able to add multiple image upload to it?thx for the script!!
| |
|
|