WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Image Upload And Resize Script
Categories : PHP, Graphics, GD image library Click here to Update Your Picture
Nick Wilson
Date : Jul 19th 2004
Grade : 4 of 5 (graded 19 times)
Viewed : 56755
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Nick Wilson
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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
Display a bar chart based on random values.
Categories : Graphics, PHP, GD image library, Charts and Graphs
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
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
CAPTCHA[Image verification]
Categories : PHP, Security, GD image library, Graphics, Sessions
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
Create a Thumbnail Using PHP, GD and Exif
Categories : PHP, Graphics, Exif, GD image library
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
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