The whole code takes an image, builds a thumbnail, saves the original and writes the links into an XML file.
Code: Select all
<html>
<head>
<title>EdSafe Upload Jpg</title>
<link type="text/css" rel="stylesheet" href="css/lightbox.css">
<?php
// Set enough memory aside to load larger images
//ini_set("memory_limit", "134217728");
//ini_set('gd.jpeg_ignore_warning', 1);
/* here we must specify the version of XML : i.e: 1.0 */
$xml = new DomDocument('1.0');
$xml->load('ceramics2.xml');
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
function resize($img, $thumb_width, $newfilename, $filename)
{
$max_width=$thumb_width;
//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}
//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);
switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;
/*** calulate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);
while($thumb_height>$max_width)
{
$thumb_width-=10;
$thumb_height = round($thumb_width * $aspect_ratio);
}
$newImg = imagecreatetruecolor($thumb_width, $thumb_height);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
// Generate a random file name
$the_date = getdate(date("U"));
$random_name_th = "th_".$the_date[0].$filename;
$random_name = $the_date[0].$filename;
//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($newImg,$random_name_th);
imagegif($im,$random_name);
break;
case 2: imagejpeg($newImg,$random_name_th);
imagejpeg($im,$random_name);
break;
case 3: imagepng($newImg,$random_name_th);
imagepng($im,$random_name);
break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
return $random_name;
}
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$random_name = resize($imgfile, 100, "th_".$imgfile_name, $imgfile_name);
//Add the image to the XML document
$newLibraryImage = $xml->createElement('libraryimage');
//Create the name element
$newImageName = $xml->createElement('name');
$newImageNameNode = $xml->createTextNode ("");
$newImageName -> appendChild($newImageNameNode);
//Create the link element
$newImageLink = $xml->createElement('link');
$newImageLinkNode = $xml->createTextNode ($random_name);
$newImageLink -> appendChild($newImageLinkNode);
//Create the thumb element
$newThumbLink = $xml->createElement('thumb');
$newThumbLinkNode = $xml->createTextNode ('th_'.$random_name);
$newThumbLink -> appendChild($newThumbLinkNode);
$newLibraryImage -> appendChild($newImageName);
$newLibraryImage -> appendChild($newImageLink);
$newLibraryImage -> appendChild($newThumbLink);
$XMLlibrary = $xml->getElementsByTagName('library')->item(0);
$XMLlibrary -> appendChild ($newLibraryImage);
$xml->save("ceramics2.xml");
}
?>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
</head>
<body bgcolor="#FFFFFF">
<table>
<tr>
<?php foreach($xml->getElementsBytagName('libraryimage') as $libraryimage):
/* find the thumb image */
$ithumb = $libraryimage->getElementsByTagName('thumb')->item(0)->firstChild->nodeValue;
$ilink = $libraryimage->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
?>
<td><a href="<?php echo($ilink) ?>" rel="lightbox[group]"><img src="<?php echo($ithumb) ?>"/></a></td>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach($xml->getElementsBytagName('libraryimage') as $libraryimage):
/* find the title */
$iname = $libraryimage->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
?>
<td><?php echo($iname) ?></td>
<?php endforeach; ?>
</tr>
</table>
<h2>Upload and Resize an Image</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
</body>
</html>