CodeCharge Studio
search Register Login  

Web Reporting

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

YesSoftware Forums -> CodeCharge Studio -> PHP

 File upload manipulation

Print topic Send  topic

Author Message
monkeyboy71

Posts: 7
Posted: 11/08/2004, 12:25 PM

Folks,

I had the need to create a thumbnail of images uploaded via a form build with CCS. I found that the best bet was putting my code in the image file events:

AfterProessFIle - for creating/updating thumbnail
BeforeDeleteFile - to delete thumbnail

The only issue I came across was getting the file name. In ASP you can reference the file name with no problem. But in PHP there is not built in fuction or reference that I could find to use to get the file's name so as to delete it's respective thumbnail.

So I looked through the FileUpload class and found that the name is there but there's just no pre-estabished function to retrieve it. So here' how you get it:

$fileName = $form->field->State[0];

I figured i'd pass that along in cause anyone else was having the same problem. Or if someone knows a better way to make this happen I'd love to see what I overlooked.

Cheerks.
View profile  Send private message
datadoit.com
Posted: 11/09/2004, 9:35 AM

Thanks for the tip!

Now I'd love to see how you did the entire thing: Upload image, create
thumbnail of image, delete images including the thumbnail. In PHP. Wanna
share or sell?

mike@datadoit.com

monkeyboy71

Posts: 7
Posted: 11/09/2004, 12:52 PM

Sure can....

To Common.php I added this function:

// 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);
}
}
}


This is the same function you will find elsewhere on the CC forums. I had problems getting ImageGIF to work so i just replaced it with ImageJPEG and it works fine.

Now, from the filed upload field I have 2 events...

AfterProcessFile event...

global $content;
$new_width = 100;
$file = $content->image1->GetValue();
$folder = $content->image1->FileFolder;
$temp_folder = $content->image1->TemporaryFolder;
KBThumbnail($file,$folder,$folder,CCGetSession("ThumbnailPrefix"),$new_width,'100');

BeforeDelete event....

global $content;
$file = $content->image1->State[0];
$folder = $content->image1->FileFolder;
$delete = $folder.CCGetSession("ThumbnailPrefix").$file;
unlink($delete);


Just change the name of your form, you image field names, and the thumbnail $new_width size in the BeforeProcessFile info and you are golden.

Holler if I can help,
Kyle
View profile  Send private message
datadoit.com
Posted: 11/09/2004, 8:18 PM

Excellent! Thanks for sharing.


> Just change the name of your form, you image field names, and the
> thumbnail
> $new_width size in the BeforeProcessFile info and you are golden.
>
> Holler if I can help,
> Kyle
> ---------------------------------------

troy
Posted: 06/09/2005, 2:36 AM

hi there:

i am using CCS,PHP,MYSQL

I have put the code in its places , but i get an error which says :

Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting T_STRING or T_VARIABLE or '{' or '$' in c:\easyphp1-71\www\thompsons2\admin\AdmProductRecord_events.php on line 42

Fatal error: Call to a member function on a non-object in c:\easyphp1-71\www\thompsons2\admin\AdmProductRecord_events.php on line 21

i no it must be something simple.
The image gets uploaded to the details folder alright, but does no make a thumbnail, is it ment to do that?.
any help would be grat thank you...
troy
Posted: 06/09/2005, 3:24 AM

hi there:
i have fixed that error.

Notice: Undefined property: FileFolder in c:\easyphp1-71\www\thompsons2\admin\AdmProductRecord_events.php on line 22

Notice: Undefined property: TemporaryFolder in c:\easyphp1-71\www\thompsons2\admin\AdmProductRecord_events.php on line 23

Fatal error: Call to undefined function: kbthumbnail() in c:\easyphp1-71\www\thompsons2\admin\AdmProductRecord_events.php on line 24

i just need a little help in setting these propertys..
thank you very much
troy
Posted: 06/09/2005, 4:12 AM

hi there;

i was wondering , do i need to change the "ThumbnailPrefix"

it works as in it uploads the large image, but does not make a thumbnail image
???

the only error i get now is:

Fatal error: Call to undefined function: kbthumbnail() in c:\easyphp1-71\www\thompsons2\admin\AdmProductRecord_events.php on line 24

cheers to anyone who can help..
monkeyboy71

Posts: 7
Posted: 06/09/2005, 10:02 AM

No, you don't need to change the ThumbnailPrefix unless you want the thumbnails called something different.

About your error...

It looks like your AdmProductRecord_events is not finding the thumbnail function. You did add that function to your Common.php file, right?

You could just put it on your events page to test it out otherwise. But Common.php is the best place for it because you can use it elsewhere in your application.
View profile  Send private message
troy
Posted: 06/09/2005, 8:35 PM

hi monkeyboy71.

yes i have added the code to the common.php file.

i dont get any errror now but when I upload an image into my folder it only makes one copy, in other words the only image in there is the one i uploaded, no thumbnail image is there...

So when i test the site the image shows up but its the big picture only. if you no what i mean..

thanks for getting back to me..
You see i have a catalog web site wich i want to show a thumbnail image , and when you click on it it then shows the large image.

I can do it buy having 2 upload fields but my client only wants to upload 1 image, so it makes a thumbnail..

I also can make it work if I set the size of the image field but then the image looks warped ..

I no it must be something simple im not setting right.
cheers..
troy
Posted: 06/09/2005, 8:49 PM

hi there..
this is where i need help i think..
this code i have put on the After Process:

//Custom Code @24-17AC310D
// -------------------------
global $store_products;
$new_width = 100;
$file = $store_products->image_url->GetValue();
$folder = $store_products->FileUpload1->FileFolder;
$temp_folder = $store_products->FileUpload1->TemporaryFolder;
KBThumbnail($file,$folder,$folder,CCGetSession("ThumbnailPrefix"),$new_width,'100');
// -------------------------
//End Custom Code

where it says image_url . thats the control source
and the Fileupload1 is the upload field on the form.

and storeproducts is the name of the forum...

cheers
monkeyboy71

Posts: 7
Posted: 06/09/2005, 10:45 PM

The reason why you are not getting an error is because the function checks to see if $file is valid, and if it's not (eg it's blank), it just exits gracefully.

Your $file value is blank because of an issue is with these lines...

$file = $store_products->image_url->GetValue();
$folder = $store_products->FileUpload1->FileFolder;

"image_url" and "FileUpload1" should be the same thing. It's the same object from which you want to gather the inputed value (GetValue()) and the programmed value for it's location (FileFolder).

So whatever your field name is what you want to plug into both locations like so...

$file = $store_products->YOURFIELDNAME->GetValue();
$folder = $store_products->YOURFIELDNAME->FileFolder;
View profile  Send private message
troy
Posted: 06/09/2005, 11:12 PM

hi monkeyboy71

$file = $store_products->image_url->GetValue();
$folder = $store_products->image_url->FileFolder;
$temp_folder = $store_products->image_url->TemporaryFolder;

i have changed it to this... image_url is the the control source "field name" in the database...

so this should create a thumbnail of the same image in the same folder ?
if i want to put the main code in an event would it go in the before show?

it still donest, but will keep trying anyway...
thanks very much for your help, anyway

monkeyboy71

Posts: 7
Posted: 06/10/2005, 11:17 AM

Do some print statements and see if you are getting any values back.

global $store_products;
$new_width = 100;
$file = $store_products->FileUpload1->GetValue();
echo "file: " . $file;
$folder = $store_products->FileUpload1->FileFolder;
echo "<br>folder: " . $folder;
$temp_folder = $store_products->FileUpload1->TemporaryFolder;
echo "<br>folder: " . $folder;
exit;

If those values are not coming through then double check that "FileUpload1" is your objects name (image_url maybe?). Either way, this will tell you if you are getting the values you need before calling the function.

God bless the print statements.
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.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


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