CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Tips & Solutions

 File upload and create thumbnail images

Print topic Send  topic

Author Message
djgjohng

Posts: 28
Posted: 10/25/2006, 10:35 PM


This is based on an old post but no doubt people are wanting to do this type of thing. For newbies like me especially I hereby throw in my two-cents worth:

Thanks to monkeyboy71.


STEP 1: The following works fine. Place this code in Common.php just before end PHP tag.


// takes in a JPEG or GIF and creates a thumbnail named thumb_NAME
function KBThumbnail($entry,$src,$path,$prefix,$new_width,$qual)
{
if(eregi(".+\.jpe?g$",$entry)){
if(!file_exists("$path/$prefix".$entry)){
$src=ImageCreateFromJPEG("$src/$entry");
$org_h=imagesy($src);
$org_w=imagesx($src);
$nwidth=$new_width;
$scale=$nwidth/$org_w;
$nheight=round($org_h*$scale);
$img=ImageCreateTrueColor($nwidth,$nheight);
ImageCopyResized($img,$src,0,0,0,0,$nwidth,$nheight,$org_w,$org_h);
$new_src=($path.$prefix.$entry);
ImageJPEG($img,$new_src,$qual);
ImageDestroy($img);
ImageDestroy($src);
}
}elseif(eregi(".+\.gif$",$entry)){
if(!file_exists("$path/$prefix".$entry)){
$src=ImageCreateFromGIF("$src/$entry");
$org_h=imagesy($src);
$org_w=imagesx($src);
$nwidth=$new_width;
$scale=$nwidth/$org_w;
$nheight=round($org_h*$scale);
$img=imagecreate($nwidth,$nheight);
ImageCopyResized($img,$src,0,0,0,0,$nwidth,$nheight,$org_w,$org_h);
$new_src=($path.$prefix.$entry);
ImageJPEG($img,$new_src,$qual);
//ImageGIF($img,$new_src,75);
ImageDestroy($img);
ImageDestroy($src);
}
}
}


STEP 2:

The thumbnail image is created after the main image is uploaded so the time stamp is different. This makes displaying the thumbnail difficult. You'll see why if you don't do the suggested method.

You can modify the following line in 'Classes.php' to add a plain date prefix without the time or just make the stored file name the actual file name as follows. "$ActualFileName = $FileName;" can cause a problem with duplicate file names so a date prefix of some sort would be best in most environments. "$ActualFileName = date("Ymd") . $index . "." . $FileName;" prefixes with a date.

(Thanks to Don Safar)


// $ActualFileName = date("YmdHis") . $index . "." . $FileName;
// $ActualFileName = date("Ymd") . $index . "." . $FileName;

$ActualFileName = $FileName;


STEP 3: If you have not done so already, add a file upload form component. It will automatically be named FileUpload1. You will need to set a control source for the field where the file name will be stored in your database. (In my case it is 'imagefile')


STEP 4: The above function requires that you specify a thumbnail prefix. (monkeyboy71 forgot to mention)

Add this custom code to an 'After Process File' event to the FileUpload1 component:

IMPORTANT:
$image_details must be changed to whatever your image upload form is named
FileUpload1 is the name of the file upload form 'component' (No need to change this name)
$new_width = 110; (Change to the thumbnail width you require)


// the code 1
global $image_details;
$new_width = 110;
$file = $image_details->FileUpload1->GetValue();
$folder = $image_details->FileUpload1->FileFolder;
$temp_folder = $image_details->FileUpload1->TemporaryFolder;
$tnail = "thumb_";

KBThumbnail($file,$folder,$folder,$tnail,$new_width,'100');


STEP 4:

Add this custom code to a 'Before Delete' event to the FileUpload1 component:

IMPORTANT:
$image_details must be changed to whatever your image upload form is named
FileUpload1 is the name of the file upload form 'component'


// the code 2
global $image_details;
$file = $image_details->FileUpload1->State[0];
$tnail = "thumb_";
$folder = $image_details->FileUpload1->FileFolder;
$delete = $folder.$tnail.$file;
unlink($delete);


STEP 5: If you want to see the images, you will need to create a record grid. Include the record primary 'ID' and maybe the file name field. Then add two image controls in new columns [IMAGE_FULL] [IMAGE_THUMB]. Make the 'image_full' control an image control and the 'image_thumb' control an image link so you can pop it up in a window if desired. (Create a new page for this purpose and set the href source to that page. Google some free javascript code for creating pop-up windows.)

[Grid]
[ID] [IMAGEFILE] [IMAGE_FULL] [IMAGE_THUMB]

Set the [IMAGE_FULL] control source to your image database field. Set the [IMAGE_THUMB] control source to 'Code Expression'.


STEP 6: Add a "Before Show" event - custom code as follows:

IMPORTANT:
$image_list must be changed to whatever your grid is named
imagefile is the name of the field control where the main image name is stored
image_thumb is the name of the image link control to be used to display the thumbnail image


// the code 3

global $image_list;

$tnail = "thumb_";
$image_record = $image_list->imagefile->GetValue();
$thumbnail_file = "$tnail" . "$image_record";

$image_list->image_thumb->SetValue($thumbnail_file);


STEP 7: In HTML find the image controls and prefix them with the path to the folder where the images are stored.

<td><img src="YOURIMAGESFOLDER/{image_full}"></td>
<td><a href="{image_thumb}"><img src="YOURIMAGESFOLDER/{image_thumb_Src}" border="0"></a></td>



And Voila! with a bit of luck you have a working system. If anyone has comments, corrections or improvements - fire away. Cheers.

View profile  Send private message
fly_mo


Posts: 109
Posted: 10/29/2006, 10:16 AM

Hi djgjohng.
Thanks for posting this....been trying to get this to work. I followed steps 1-4 (I just want to upload and show on the current page for now) When I try to upload, I get the following error:

Fatal error: Call to undefined function ImageCreateFromJPEG() in C:\wamp\www\EAPGS\members\Common.php on line 1907

Any ideas that may help.
I didn't quite understand step 2 - so I didn't edit the classes.php (could this be part of the problem?)
Best Regards
John

Also when I delete the pic I get the following - I'm doing something wrong with the folder?

Warning: unlink(./thumb_200610291313460.CEarn.jpg) [function.unlink]: No such file or directory in C:\wamp\www\EAPGS\members\MyDetails_events.php on line 221

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\EAPGS\members\MyDetails_events.php:221) in C:\wamp\www\EAPGS\members\MyDetails.php on line 1095
_________________
John
View profile  Send private message
AlexR

Posts: 8
Posted: 10/30/2006, 7:18 AM

First, your PHP doesn't seem to include GD extension (needed to work with images). You should be able to uncomment it in your php.ini.
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.