hello,
heres some of the code that i used to upload and size images,
it might take a bit of deciphering, and i have removed the gif resizing part,
but basically everything here is built up from examples on how to use the GD graphics library
cheers,
mat
if ($userChoice == "Upload")
{
if ($_FILES['upload'] != "")
{
//echo "temp name : " .$_FILES['upload']['tmp_name'] ."<br>";
//echo "size : " .$_FILES['upload']['size'] ."<br>";
//echo "type : " .$_FILES['upload']['type'] ."<br>";
//echo "name : " .$_FILES['upload']['name'] ."<br>";
$imagesize = getimagesize($_FILES['upload']['tmp_name']);
//echo $imagesize[0]."<br>";
//echo $imagesize[1]."<br>";
//echo $imagesize[2]."<br>";
if ($imagesize[2] == 1)
{
$allowed = true;
$imageFormat = "gif";
}
else if ($imagesize[2] == 2)
{
$allowed = true;
$imageFormat = "jpg";
}
else
{
$allowed = false;
}
if (!$allowed)
{
$this->HTML = "The image cannot be uploaded. Only 'gif' and 'jpg' format images can be uploaded";
}
else
{
$width = $imagesize[0];
$height =$imagesize[1];
//start transaction
$query = "BEGIN";
$result = mysql_query($query);
//MAKE ENTRY IN MEDIA STORAGE TABLE
$query = "INSERT INTO image (width, height, caption) values (". $width.", ". $height .",'".$caption."')";
$result = mysql_query($query);
if(($result===false))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Information on the item could not be saved.</p>");
return;
}
//MAKE FILE NAME USING UNIQUE ID
$mediaStorageId = mysql_insert_id();
$imageName = "image_".$page. "_" .$mediaStorageId ;
$imageFilename = $imageName ."." .$imageFormat;
$image_save_directory = getSetting("standard_image_save_folder");
$thumbnail_save_directory = getSetting("thumbnail_image_save_folder");
$image_directory = getSetting("standard_image_folder");
$thumbnail = "";
$copyWidth = 0;
$copyHeight = 0;
//SAVE FILE
if (!copy($_FILES['upload']['tmp_name'], $image_save_directory.$imageFilename))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>The file could not be saved.</p>");
return;
}
$sourceImage = "";
if ($imageFormat === "jpg")
{
$sourceImage = imagecreatefromjpeg($_FILES['upload']['tmp_name']);
}
if(strlen($sourceImage) > 0)
{
$thumbnail_width = getSetting("thumbnail_width");
$thumbnail_height = getSetting("thumbnail_height");
$quality = 100;
if (($width > $thumbnail_width) or ($height > $thumbnail_height))
{
if (($width / $height) > ($thumbnail_width / $thumbnail_height))
{
$copyWidth = $thumbnail_width;
$copyHeight = round ($height * ($thumbnail_width/$width));
if ($copyHeight > $thumbnail_height)
{
$copyHeight = $thumbnail_height;
}
else if ($copyHeight < 1)
{
$copyHeight = 1;
}
}
else
{
$copyHeight = $thumbnail_height;
$copyWidth = round ($width * $thumbnail_height/$height);
if ($copyWidth > $thumbnail_width)
{
$copyWidth = $thumbnail_width;
}
else if ($copyWidth < 1)
{
$copyWidth = 1;
}
}
}
else
{
$copyHeight = $height;
$copyWidth = $width;
}
$destinationImage = imagecreatetruecolor($copyWidth,$copyHeight);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $copyWidth, $copyHeight, $width, $height);
imagejpeg($destinationImage, $thumbnail_save_directory.$imageName.".jpg", 100);
imagedestroy($destinationImage);
imagedestroy($sourceImage);
}
//the code from now on just refers to my
//cms, the point being that
if you store the filename, and hieght and width date in the database you can auto generate html to show images
//ENTER FILE NAME IN MEDIA STORAGE TABLE
$query="UPDATE image SET fileName='".$imageFilename."'"
. ", thumbnailFileName ='".$imageName.".jpg'"
. ", thumbnailHeight =".$copyHeight
. ", thumbnailWidth =".$copyWidth
. " WHERE id = ". $mediaStorageId;
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>The item details could not be saved.</p>");
return;
}
//GET NEXT POSITION IN PAGE
// select the last position so
// next position can be calculated
$query="SELECT * FROM pagecontent "
. " WHERE pageId = ". $page
. " AND parentId = " .$item
. " AND position IS NOT NULL"
. " ORDER BY position DESC LIMIT 1";
$result = mysql_query($query);
if(($result===false))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Information on the specified page could not be found.</p>");
return;
}
//no content found
if (!$row = mysql_fetch_array($result))
{
$nextPosition = 1;
}
//content found, add 1 to last position
else
{
$nextPosition = 1 + $row['position'];
}
//MAKE PAGE CONTENT ENTRY
$moduleNumber = getModuleNumber("Image");
$mediaTypeId = getMediaTypeId("image");
if(($moduleNumber===false) or ($mediaTypeId===false))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Information on item handling could not be found.</p>");
return;
}
$query = "INSERT INTO pagecontent(pageId, parentId, position, module) values (".$page .",".$item ."," .$nextPosition .",". $moduleNumber.")";
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Item could not be added to the page.</p>");
return;
}
else
{
$pageContentId = mysql_insert_id();
}
//MAKE PAGE CONTENT MEDIA ENTRY
$query = "INSERT INTO pagecontentmedia(pageContentId, mediaType, mediaStorageId) values (".$pageContentId .",". $mediaTypeId. ",". $mediaStorageId .")";
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Item storage information could not be saved.</p>");
return;
}
//MAKE PAGE CONTENT SETTING ENTRY
$query = "INSERT INTO pagecontentsetting(pageContentId, name, value) values (".$pageContentId .",'frame','".$frame."')";
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Item settings could not be saved.</p>");
return;
}
//MAKE PAGE CONTENT SETTING ENTRY
$query = "INSERT INTO pagecontentsetting(pageContentId, name, value) values (".$pageContentId .",'caption','default')";
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Item settings could not be saved.</p>");
return;
}
//MAKE PAGE CONTENT SETTING ENTRY
$query = "INSERT INTO pagecontentsetting(pageContentId, name, value) values (".$pageContentId .",'thumb','".$thumb."')";
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Item settings could not be saved.</p>");
return;
}
if (($thumb === "thumb") and ($linkType === "image"))
{
//MAKE PAGE CONTENT SETTING ENTRY
$query = "INSERT INTO pagecontentsetting(pageContentId, name, value) values (".$pageContentId .",'linkType','image')";
$result = mysql_query($query );
if(($result===false) or (mysql_affected_rows()<1))
{
$query = "ROLLBACK";
$result = mysql_query($query);
$pageObject->sendError("<p class='basicText'>Item settings could not be saved.</p>");
return;
}
}
//it worked
$query = "COMMIT";
$result = mysql_query($query);
}
}
}