Results 1 to 8 of 8

Thread: [RESOLVED] Completely stumped

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Resolved [RESOLVED] Completely stumped

    Hello all.
    I have this script which scales an image. It works fine on my home computer, but when I take it to my system at work, PHP merely displays the URL of the page. The web app and the MySQL database are exact copies of each other. Perhaps someone can tell me what is happening here?
    Many many thanks to anyone who can help.
    PHP Code:
    <?php

        
    /* THE PURPOSE OF THIS FILE IS TO UPLOAD, THEN SCALE AN IMAGE, IF NEEDED */


        /* SECURITY LOCAL TO THIS SCRIPT */
            
    if(
                isset(
    $GLOBALS['post']['id'])
                &&
                isset(
    $_FILES['txt_file'])
            ) {

                
    /* THE USER HAS PASSED */


                /* UPLOAD THE IMAGE, IF APPLICABLE */
                    
    if(
                        
    strlen($_FILES['txt_file']['name'])>0
                    
    ) {
                        
    /* SPLIT THE POSTED FILE NAME */
                            
    $split_name=explode("."$_FILES['txt_file']['name']);


                        
    /* SET THE FILE EXTENSION */
                            
    $file_extension='.'.end($split_name);


                        
    /* NEW FILENAME */
                            
    $new_file_name=$GLOBALS['post']['id'].'_'.mktime().$file_extension;


                        
    /* READ THE TEMPORARY FILE */
                            
    $temp_name $_FILES['txt_file']['tmp_name'];
                            
    $temp_file fopen($temp_name"r");
                            
    $binary_data fread($temp_filefileSize($temp_name));


                        
    /* TURN ON ERROR REPORTING */
                            
    $old_error_reporting error_reporting(E_ALL & ~(E_WARNING));


                        
    /* CREATE THE FILE */
                            
    $file_path '../_upload/'.$new_file_name;
                                
    touch($file_path);
                                    
    $fp=fopen($file_path'w');
                                        
    fwrite($fp$binary_data);
                                    
    fclose($fp);


                        
    /* TURN OFF ERROR REPORTING */
                            
    error_reporting($old_error_reporting);


                        
    /* CHECK THE IMAGE DIMENSIONS */
                            
    $image=getimagesize($file_path);
                                
    $old_width=$image[0];
                                
    $old_height=$image[1];


                        
    /* NEEDED FOR RESIZING IMAGES */
                            
    if(
                                
    $old_width>375 || $old_height>375
                            
    ) {
                                
    /* NEED TO DETERMINE WHICH IS GREATER, HEIGHT OR WIDTH */
                                    
    if(
                                        
    $old_height>$old_width
                                        
    ||
                                        
    $old_height==$old_width
                                    
    ) {
                                        
    $divider=ceil($old_height/375);
                                    } else {
                                        
    $divider=ceil($old_width/375);
                                    }
                                
    /* NOW SET THE $new_height AND $new_width VARIABLES */
                                    
    $new_height=floor($old_height/$divider);
                                    
    $new_width=floor($old_width/$divider);
                            } else {
                                
    $new_height=$old_height;
                                
    $new_width=$old_width;
                            }


                        
    /* INITIALIZE THE SOURCE AND OUTPUT FILE NAMES */
                            
    $source_file_name='../_upload/'.$new_file_name;
                            
    $output_file_name='../../_images/_products/'.$new_file_name;
                            
    $thumbnail_file_name='../../_images/_products_thumbnails/'.$new_file_name;


                        
    /* THE CONTENT TYPE */
                            
    if($file_extension=='.jpg') {
                                
    header('Content-type: image/jpeg');
                            }


                        
    /* FOUND THE FOLLOWING FIX HERE */
                        /* http://worcesterwideweb.com/2008/03/17/php-5-and-imagecreatefromjpeg-recoverable-error-premature-end-of-jpeg-file/ */
                            
    ini_set('gd.jpeg_ignore_warning'1);


                        
    /* RESAMPLE THE IMAGE */
                            
    $image_p=imagecreatetruecolor($new_width$new_height);
                            
    $image=imagecreatefromjpeg($source_file_name);
                                
    imagecopyresampled($image_p$image0000$new_width$new_height$old_width$old_height);


                        
    /* CREATE THE IMAGE */
                            
    if($file_extension=='.jpg') {
                                
    imagejpeg($image_p,$output_file_name,100);
                            }


                        
    /* RESAMPLE THE IMAGE */
                            
    $image_p2=imagecreatetruecolor(100100);
                            
    $image=imagecreatefromjpeg($source_file_name);
                                
    imagecopyresampled($image_p2$image0000100100$old_width$old_height);


                        
    /* CREATE THE THUMBNAIL IMAGE */
                            
    if($file_extension=='.jpg') {
                                
    imagejpeg($image_p2,$thumbnail_file_name,100);
                            }


                        
    /* CALL THE CLASS FUNCTION TO BUILD A QUERY TO UPDATE A PRODUCT IMAGE */
                            
    if($file_extension=='.jpg') {
                                
    $GLOBALS['cms_queries']->update_product_image($GLOBALS['post']['id'],$new_file_name);
                            }


                        
    /* SINCE I'VE ALREADY SENT HEADERS I NEED TO REDIRECT MANUALLY */
                            
    echo '<html>';

                                echo 
    '<head>';

                                    echo 
    '<title>REDIRECTING</title>';

                                    echo 
    '<meta http-equiv="refresh" content="0;url=../'.$GLOBALS['global_admin_page'].'?a=040&red=041&thank='.mktime().'" />';

                                echo 
    '</head>';

                                echo 
    '<body>';

                                echo 
    '</body>';

                            echo 
    '</html>';


                    }

            }

    ?>
    Last edited by solitario; Sep 30th, 2009 at 12:40 PM. Reason: [RESOLVED]

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Completely stumped

    the GD extension is installed on the server you're having trouble with, right?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: Completely stumped

    Yes, it is installed.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Completely stumped

    I don't know exactly why the script doesn't work, but it's so riddled with misconceptions and poor practices that it's hard to know where to begin. Why is $GLOBALS used? Why the error reporting level changed mid-script? Why is the binary data read in and then written out, instead of using move_uploaded_file? Why is echo used to output HTML? Why is HTML used to do a redirection that should be done with a header? Why is HTML emitted after setting a content type of image/jpeg?

    To be honest, it's hard to imagine it working at all. Start again.


    Edit; Also, there are far too many comments and they are mostly misleading.
    Last edited by penagate; Sep 29th, 2009 at 09:10 PM.

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Completely stumped

    alright. your script works fine (as messy as it is), but the problem is due to the header you're sending. you are saving all of your image files rather than outputting them to the browser; this means that PHP sees that you're trying to output an image (by sending a header of 'image/jpeg'), but you're not using imagejpeg() to output an image (you're saving it instead). just get rid of the header you're sending.

    and get rid of that disgusting HTML you're outputting! PHP is an embedded language, and you should treat it like one. example:
    PHP Code:
    <?php

      
    //php code

    ?>
    <!-- html -->
    <?php

      
    //more php code

    ?>
    <!-- more html -->
    edit: didn't see penagate's post before I made mine, but all of the things he mentioned would definitely make your code much easier to understand. not to mention more efficient and a lot less work [on your part, as far as writing goes].
    Last edited by kows; Sep 29th, 2009 at 09:33 PM.

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Completely stumped

    @ solitario,

    You say that there is a database but there is not mention of it in that code! Does the file get uploaded to the database? If so you need to tell us what field to put in the database.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Completely stumped

    if you looked at his code, he is obviously not putting the image in a database. the query he uses is a call to a class that can simply be removed from this script to run. it merely logs the uploaded file's name.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: Completely stumped

    ouch...
    yeah, thanks folks. cleaning it up a bit fixed it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width