SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Thread: Shrink Image?

  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question Shrink Image?

    I found a great script on the web to easily shrink an image to a prefered size.
    I'm helping a friend to make him put his images in the right format on his web page and try to do it easy for him.
    This was a great solution. I choose a file and then it is resized by this script and placed in the same folder.
    But I would like this script to keep the original name of the file and store it in a folder called img (in the same directory).
    But I can't figure out how to do it.

    Cause, then I will have a link to this image from a MySQL db and show his images in a table.
    And this script will then make the files within the right size.

    Does anyone know? I'm not that good yet, but hope I will be one day.

    I've attached the files as a zipped file.
    Since it's so long I guess it's the easiest way here, right?

    Brad
    Attached Files Attached Files

  2. #2
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your file is still awaiting approval (as are a few of mine from a day or so ago) - maybe upload somewhere and post a link to it instead?

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    I'll add the code instead.

    shrinkimage.php looks like this (function.php is added after):


    PHP Code:
    <?

    /*
     * This script is copyright PHPGarage.com (On Line Construction Inc.). It may be used,
     * changed, and distributed freely as long as this message and/or some type of recognition
     * is given to PHPGarage.com or On Line Construction Inc.
     * 
     * http://www.phpgarage.com
     * http://www.onlineconstructioninc.com
     *  
     */

    // Filename to store image as (no extention)
    $FILENAME="image_name";

    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=400;

    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=400;

    // DO NOT EDIT BELOW HERE -----------------------------------------

    include("function.php");

    if(
    $_FILES['image']['size']){
        if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
            
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']);
        }elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
            
    $im imagecreatefrompng($_FILES['image']['tmp_name']);
        }elseif(
    $_FILES['image']['type'] == "image/gif"){
            
    $im imagecreatefromgif($_FILES['image']['tmp_name']);
        }
        if(
    $im){
            if(
    file_exists("$FILENAME.jpg")){
                
    unlink("$FILENAME.jpg");
            }
            
    ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
            
    ImageDestroy ($im);
        }
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    </head>
    <body>

    <br><br>

    <img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>

    <form enctype="multipart/form-data" method="post">
    <b>Resize Image</b><br>
    <input type="file" name="image" size="50"><br><br>
    <input type="submit" value="Upload Photo">
    </form> 

    </body>
    </html>

    The next file (function.php) looks like the following:



    PHP Code:
    <?

    /*
     * This script is copyright PHPGarage.com (On Line Construction Inc.). It may be used,
     * changed, and distributed freely as long as this message and/or some type of recognition
     * is given to PHPGarage.com or On Line Construction Inc.
     * 
     * http://www.phpgarage.com
     * http://www.onlineconstructioninc.com
     *  
     */

    function ResizeImage($im,$maxwidth,$maxheight,$name){
        
    $width imagesx($im);
        
    $height imagesy($im);
        if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){
            if(
    $maxwidth && $width $maxwidth){
                
    $widthratio $maxwidth/$width;
                
    $RESIZEWIDTH=true;
            }
            if(
    $maxheight && $height $maxheight){
                
    $heightratio $maxheight/$height;
                
    $RESIZEHEIGHT=true;
            }
            if(
    $RESIZEWIDTH && $RESIZEHEIGHT){
                if(
    $widthratio $heightratio){
                    
    $ratio $widthratio;
                }else{
                    
    $ratio $heightratio;
                }
            }elseif(
    $RESIZEWIDTH){
                
    $ratio $widthratio;
            }elseif(
    $RESIZEHEIGHT){
                
    $ratio $heightratio;
            }
            
    $newwidth $width $ratio;
            
    $newheight $height $ratio;
            if(
    function_exists("imagecopyresampled")){
                  
    $newim imagecreatetruecolor($newwidth$newheight);
                  
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height);
            }else{
                
    $newim imagecreate($newwidth$newheight);
                  
    imagecopyresized($newim$im0000$newwidth$newheight$width$height);
            }
            
    ImageJpeg ($newim,$name ".jpg");
            
    ImageDestroy ($newim);
        }else{
            
    ImageJpeg ($im,$name ".jpg");
        }
    }

    ?>

  4. #4
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The line that saves the file is in the function.php:

    ImageJpeg ($newim,$name . ".jpg");

    Where $name = the name and location of the file to be saved. $name is passed into the function from the other script. You should be able to simply do this:

    1) Change this line: ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME); so it passes over the actual file name, i.e.:

    ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$_FILES['image']['name']);

    2) And change this line in function.php: ImageJpeg ($newim,$name . ".jpg"); so it has the directory prepended to it:

    ImageJpeg ($newim, "dir/$name" . ".jpg");

    Some things to look out for - using this method will quite likely mean you still have the extension of the file you upload. For example you may get something like: photo.gif.jpg as a final file name! If that's the case you need to modify the code a little, but the above should certainly do what you asked.

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It seems to work, but one pretty important thing.
    When I choose a file and upload it, I used to see what it looked like after uploading it (in the same window, but with the new size).
    Now I won't see the new image at all. I just pick a file and press to upload and never see the image.
    But it ends up in the correct folder and with the correct name, but can't see what happened during the process...
    Maybe I need to add something to show the file from the folder aas well or what?

    Brad

  6. #6
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No you just need to change the <img> tag in the HTML.

    <img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>

    Well, it's not in the root directory any more and $filename isn't assigned anywhere. Easiest way - stick the dir/ in in the img src and add this to the function.php:

    global $FILENAME

    (put as the first thing inside the function)

    Then at the end of the function just do $FILENAME = $name;

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Maybe I misunderstood something, but it doesn't work anyway.
    I have the files in a folder called package
    I have the images in a folder just above this called img

    So from the root I have a folder for my friend where I play around.
    His folder here is called mp.
    In mp I have one called img and one called package.
    Maybe I have the pointing to these done the wrong way or what?

    Here is the script like it looks right now after the modifications you helped me with.


    PHP Code:
    <?

    /*
     * This script is copyright PHPGarage.com (On Line Construction Inc.). It may be used,
     * changed, and distributed freely as long as this message and/or some type of recognition
     * is given to PHPGarage.com or On Line Construction Inc.
     * 
     * http://www.phpgarage.com
     * http://www.onlineconstructioninc.com
     *  
     */

    // Filename to store image as (no extention)
    $FILENAME="image_name";

    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=200;

    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=200;

    // DO NOT EDIT BELOW HERE -----------------------------------------

    include("function.php");

    if(
    $_FILES['image']['size']){
        if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
            
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']);
        }elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
            
    $im imagecreatefrompng($_FILES['image']['tmp_name']);
        }elseif(
    $_FILES['image']['type'] == "image/gif"){
            
    $im imagecreatefromgif($_FILES['image']['tmp_name']);
        }
        if(
    $im){
            if(
    file_exists("$FILENAME.jpg")){
                
    unlink("$FILENAME.jpg");
            }
             
    ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$_FILES['image']['name']);
            
    ImageDestroy ($im);
        }
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    </head>
    <body>

    <br><br>

    <img src="<? echo(dir/mp/img/$FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>

    <form enctype="multipart/form-data" method="post">
    <b>&Auml;ndra storlek p&aring; bild </b><br>
    <input type="file" name="image" size="50"><br><br>
    <input type="submit" value="Ladda upp bild">
    </form> 

    </body>
    </html>

    And then the function file:

    PHP Code:
    <?

    /*
     * This script is copyright PHPGarage.com (On Line Construction Inc.). It may be used,
     * changed, and distributed freely as long as this message and/or some type of recognition
     * is given to PHPGarage.com or On Line Construction Inc.
     * 
     * http://www.phpgarage.com
     * http://www.onlineconstructioninc.com
     *  
     */
    global $FILENAME;
    function 
    ResizeImage($im,$maxwidth,$maxheight,$name){
        
    $width imagesx($im);
        
    $height imagesy($im);
        if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){
            if(
    $maxwidth && $width $maxwidth){
                
    $widthratio $maxwidth/$width;
                
    $RESIZEWIDTH=true;
            }
            if(
    $maxheight && $height $maxheight){
                
    $heightratio $maxheight/$height;
                
    $RESIZEHEIGHT=true;
            }
            if(
    $RESIZEWIDTH && $RESIZEHEIGHT){
                if(
    $widthratio $heightratio){
                    
    $ratio $widthratio;
                }else{
                    
    $ratio $heightratio;
                }
            }elseif(
    $RESIZEWIDTH){
                
    $ratio $widthratio;
            }elseif(
    $RESIZEHEIGHT){
                
    $ratio $heightratio;
            }
            
    $newwidth $width $ratio;
            
    $newheight $height $ratio;
            if(
    function_exists("imagecopyresampled")){
                  
    $newim imagecreatetruecolor($newwidth$newheight);
                  
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height);
            }else{
                
    $newim imagecreate($newwidth$newheight);
                  
    imagecopyresized($newim$im0000$newwidth$newheight$width$height);
            }
            
    ImageJpeg ($newim"../img/$name");
            
    ImageDestroy ($newim);
        }else{
            
    ImageJpeg ($im,$name ".jpg");
        }
    }

    $FILENAME $name;

    ?>

  8. #8
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    global keywords go INSIDE the function block. Ah nevermind, it will take too long to list out the changes so here you go - this is the only file you need, I have merged the functions.php file into the other one.

    1) ** MAKE SURE ** you change the $thumbdir path to the FULL PATH of the thumbnail directory. Do not use "../img", that isn't a full path. It will break if this isn't correct.

    2) ** MAKE SURE ** you change the image HTML tag (<img src="img/<?=$filename?>.jpg">) so the thumbnail directory is correct.

    That's it, this works perfectly here and if you do both of the above two things properly, there is no reason why it shouldn't work for you also.

    PHP Code:
    <?
    // Width to reszie image to (in pixels)
    $RESIZEWIDTH=400;

    // Width to reszie image to (in pixels)
    $RESIZEHEIGHT=400;

    //    Enter the COMPLETE PATH to the directory where the thumb-nails should be stored WITH the final slash /
    $thumbdir 'D:/usr/home/sandbox/public_html/img/';

    // DO NOT EDIT BELOW HERE -----------------------------------------

    function ResizeImage($im,$maxwidth,$maxheight,$name)
    {
        global 
    $filename;
        global 
    $thumbdir;
        
        
    $name substr($name0strlen($name)-4);

        
    $width imagesx($im);
        
    $height imagesy($im);

        if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){
            if(
    $maxwidth && $width $maxwidth){
                
    $widthratio $maxwidth/$width;
                
    $RESIZEWIDTH=true;
            }
            if(
    $maxheight && $height $maxheight){
                
    $heightratio $maxheight/$height;
                
    $RESIZEHEIGHT=true;
            }
            if(
    $RESIZEWIDTH && $RESIZEHEIGHT){
                if(
    $widthratio $heightratio){
                    
    $ratio $widthratio;
                }else{
                    
    $ratio $heightratio;
                }
            }elseif(
    $RESIZEWIDTH){
                
    $ratio $widthratio;
            }elseif(
    $RESIZEHEIGHT){
                
    $ratio $heightratio;
            }
            
    $newwidth $width $ratio;
            
    $newheight $height $ratio;
            if(
    function_exists("imagecopyresampled")){
                  
    $newim imagecreatetruecolor($newwidth$newheight);
                  
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height);
            }else{
                
    $newim imagecreate($newwidth$newheight);
                  
    imagecopyresized($newim$im0000$newwidth$newheight$width$height);
            }
            
    ImageJpeg ($newim$thumbdir $name ".jpg");
            
    ImageDestroy ($newim);
        } else {
            
    ImageJpeg ($im$thumbdir $name ".jpg");
        }

        
    $filename $name;

    }

    if (
    $_FILES['image']['size'])
    {
        if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
            
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']);
        }elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
            
    $im imagecreatefrompng($_FILES['image']['tmp_name']);
        }elseif(
    $_FILES['image']['type'] == "image/gif"){
            
    $im imagecreatefromgif($_FILES['image']['tmp_name']);
        }

        if (
    $im)
        {
            if (
    file_exists("$thumbdir/{$_FILES['image']['tmp_name']}"))
            {
                
    unlink("$thumbdir/{$_FILES['image']['tmp_name']}");
            }
            
    ResizeImage($im$RESIZEWIDTH$RESIZEHEIGHT$_FILES['image']['name']);
            
    ImageDestroy ($im);
        }
    }

    ?>
    <html>
    <head>
    <title>Resize Thing</title>
    </head>

    <body>

    <img src="img/<?=$filename?>.jpg">

    <form enctype="multipart/form-data" method="post">
    <b>Resize Image</b><br>
    <input type="file" name="image" size="50"><br><br>
    <input type="submit" value="Upload Photo">
    </form>

    </body>
    </html>
    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You've been helping me a lot here, but there seems to be something wrong anyway.
    I get the following error code (changed mydomain here):

    Parse error: parse error, unexpected T_VARIABLE in /customers/mydomain.com/mydomain.com/httpd.www/mp/package/resize.php on line 15

    And line 15 seems to be the line with the code: global $filename;

    I thought it might have something to do with globals turned off, but I checked the phpinfo and globals are on. What could be wrong then?
    That error code didn't tell me much about it... or did it?

  10. #10
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The global line is fine. What did you modify in the script I posted above before uploading it to your server? The script as it stands works perfectly on PHP 4.3.10 on my Windows and Linux servers. Post the changed file?
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is what I have now.

    :-(

    PHP Code:
    <?php
    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=400

    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=400

    //****Enter the COMPLETE PATH to the directory where the thumb-nails should be stored WITH the final slash / 
    $thumbdir '/customers/mydomain.com/mydomain.com/httpd.www'

    // DO NOT EDIT BELOW HERE ----------------------------------------- 

    function ResizeImage($im,$maxwidth,$maxheight,$name

    ****global 
    $filename
    ****global 
    $thumbdir
    ****  
     ****
    $name substr($name0strlen($name)-4); 

    ****
    $width imagesx($im); 
    ****
    $height imagesy($im); 

    ****if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){ 
    ********if(
    $maxwidth && $width $maxwidth){ 
    ************
    $widthratio $maxwidth/$width
    ************
    $RESIZEWIDTH=true
    ********} 
    ********if(
    $maxheight && $height $maxheight){ 
    ************
    $heightratio $maxheight/$height
    ************
    $RESIZEHEIGHT=true
    ********} 
    ********if(
    $RESIZEWIDTH && $RESIZEHEIGHT){ 
    ************if(
    $widthratio $heightratio){ 
    ****************
    $ratio $widthratio
    ************}else{ 
    ****************
    $ratio $heightratio
    ************} 
    ********}elseif(
    $RESIZEWIDTH){ 
    ************
    $ratio $widthratio
    ********}elseif(
    $RESIZEHEIGHT){ 
    ************
    $ratio $heightratio
    ********} 
    ********
    $newwidth $width $ratio
    ********
    $newheight $height $ratio;
     
     *******if(
    function_exists("imagecopyresampled")){ 
    **************
    $newim imagecreatetruecolor($newwidth$newheight); 
    **************
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height); 
    ********}else{ 
    ************
    $newim imagecreate($newwidth$newheight); 
    **************
    imagecopyresized($newim$im0000$newwidth$newheight$width$height); 
    ********} 
    ********
    ImageJpeg ($newim$thumbdir $name ".jpg"); 
    ********
    ImageDestroy ($newim); 
    ****} else { 
    ********
    ImageJpeg ($im$thumbdir $name ".jpg"); 
    ****} 

    ****
    $filename $name



    if (
    $_FILES['image']['size']) 

    ****if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){ 
    ********
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']); 
    ****}elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){ 
    ********
    $im imagecreatefrompng($_FILES['image']['tmp_name']); 
    ****}elseif(
    $_FILES['image']['type'] == "image/gif"){ 
    ********
    $im imagecreatefromgif($_FILES['image']['tmp_name']); 
    ****}
     

    ****if (
    $im
    ****{ 
    ********if (
    file_exists("$thumbdir/{$_FILES['image']['tmp_name']}")) 
    ********{ 
    ************
    unlink("$thumbdir/{$_FILES['image']['tmp_name']}"); 
    ********} 
    ********
    ResizeImage($im$RESIZEWIDTH$RESIZEHEIGHT$_FILES['image']['name']); 
    ********
    ImageDestroy ($im); 
    ****} 


    ?> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Resize Thing</title> 
    </head> 

    <body> 

    <img src="/mp/img/<?=$filename?>.jpg" /> 

    <form enctype="multipart/form-data" method="post"> 
    <b>Resize Image</b><br /> 
    <input type="file" name="image" size="50" />
    <br />
    <br /> 
    <input type="submit" value="Upload Photo" /> 
    </form> 



    </body>
    </html>

  12. #12
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow.. where did all those stars come from??!

    Also - read very carefully the instructions I wrote about what should be on the end of the thumbnail directory - and then check yours

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  13. #13
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I forgot to add that ending when I changed the domain name and copying the file.
    Of course I added the imf folder at the end, but it doesn't work anyway.
    The stars were added when I copied the script from Dreamweaver. Don't know why, but it's the spaces that showed up as stars.

    This is the correct code (that doesn't work).

    PHP Code:
    <?php
    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=400

    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=400

    //****Enter the COMPLETE PATH to the directory where the thumb-nails should be stored WITH the final slash / 
    $thumbdir '/customers/mydomain.com/mydomain.com/httpd.www/mp/img/'

    // DO NOT EDIT BELOW HERE ----------------------------------------- 

    function ResizeImage($im,$maxwidth,$maxheight,$name

    ****global 
    $filename
    ****global 
    $thumbdir
    ****  
     ****
    $name substr($name0strlen($name)-4); 

    ****
    $width imagesx($im); 
    ****
    $height imagesy($im); 

    ****if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){ 
    ********if(
    $maxwidth && $width $maxwidth){ 
    ************
    $widthratio $maxwidth/$width
    ************
    $RESIZEWIDTH=true
    ********} 
    ********if(
    $maxheight && $height $maxheight){ 
    ************
    $heightratio $maxheight/$height
    ************
    $RESIZEHEIGHT=true
    ********} 
    ********if(
    $RESIZEWIDTH && $RESIZEHEIGHT){ 
    ************if(
    $widthratio $heightratio){ 
    ****************
    $ratio $widthratio
    ************}else{ 
    ****************
    $ratio $heightratio
    ************} 
    ********}elseif(
    $RESIZEWIDTH){ 
    ************
    $ratio $widthratio
    ********}elseif(
    $RESIZEHEIGHT){ 
    ************
    $ratio $heightratio
    ********} 
    ********
    $newwidth $width $ratio
    ********
    $newheight $height $ratio;
     
     *******if(
    function_exists("imagecopyresampled")){ 
    **************
    $newim imagecreatetruecolor($newwidth$newheight); 
    **************
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height); 
    ********}else{ 
    ************
    $newim imagecreate($newwidth$newheight); 
    **************
    imagecopyresized($newim$im0000$newwidth$newheight$width$height); 
    ********} 
    ********
    ImageJpeg ($newim$thumbdir $name ".jpg"); 
    ********
    ImageDestroy ($newim); 
    ****} else { 
    ********
    ImageJpeg ($im$thumbdir $name ".jpg"); 
    ****} 

    ****
    $filename $name



    if (
    $_FILES['image']['size']) 

    ****if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){ 
    ********
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']); 
    ****}elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){ 
    ********
    $im imagecreatefrompng($_FILES['image']['tmp_name']); 
    ****}elseif(
    $_FILES['image']['type'] == "image/gif"){ 
    ********
    $im imagecreatefromgif($_FILES['image']['tmp_name']); 
    ****}
     

    ****if (
    $im
    ****{ 
    ********if (
    file_exists("$thumbdir/{$_FILES['image']['tmp_name']}")) 
    ********{ 
    ************
    unlink("$thumbdir/{$_FILES['image']['tmp_name']}"); 
    ********} 
    ********
    ResizeImage($im$RESIZEWIDTH$RESIZEHEIGHT$_FILES['image']['name']); 
    ********
    ImageDestroy ($im); 
    ****} 


    ?> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Resize Thing</title> 
    </head> 

    <body> 

    <img src="/mp/img/<?=$filename?>.jpg" /> 

    <form enctype="multipart/form-data" method="post"> 
    <b>Resize Image</b><br /> 
    <input type="file" name="image" size="50" />
    <br />
    <br /> 
    <input type="submit" value="Upload Photo" /> 
    </form> 



    </body>
    </html>

  14. #14
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does the web server has sufficient permissions to be able to create a file in that directory? (permissions would need to be set properly on both mp/ and img/)
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  15. #15
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The permission were set to 755 on those folders.
    I have now changed them to 777 to see what happened, but that didn't work either.
    I changed the mp, img and package folders. Just to be sure...

    Still get the following error code:
    Parse error: parse error, unexpected T_VARIABLE in /customers/mydomain.com/mydomain.com/httpd.www/mp/package/resize.php on line 15

  16. #16
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, too bad I guess. The code I posted above works perfectly here, resizes and displays the image as it should on PHP 4.3.10.

    Something is messing up the code when you import it into Dreamweaver and I don't care enough about Dreamweaver to find out what I am afraid. Use a text editor instead to test it out. I.e. copy the code into Notepad, change the $thumbdir and upload it. If it's really the "global" keyword that is screwing it then something is very wrong with the installation of PHP on your hosts servers.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  17. #17
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok. I will try on a pc when I'm home. I'm usually on a Mac (even in the office), but I also have a pc at home. I'll get back to you and tell you what happened. I guess you are curious... too.

  18. #18
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Still problem

    Hello again

    Now I've re-written the code on a text editor instead.
    And finally it uploads the files and put them in the correct folder, but
    I never get a preview of the image in my window.

    Do you see a preview of the image after it's been resized?

    I've added my script again (which is now moved up one level in the hiearchy).

    PHP Code:
    <? 
    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=200

    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=200

    // Enter the COMPLETE PATH to the directory where the thumb-nails should be stored WITH the final slash / 
    $thumbdir ='/customers/mydomain.com/mydomain.com/httpd.www/mp/pic/';

    // DO NOT EDIT BELOW HERE ----------------------------------------- 

    function ResizeImage($im,$maxwidth,$maxheight,$name

    global 
    $filename
    global 
    $thumbdir

    $name substr($name0strlen($name)-4); 

    $width imagesx($im); 
    $height imagesy($im); 

    if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){ 
    if(
    $maxwidth && $width $maxwidth){ 
    $widthratio $maxwidth/$width
    $RESIZEWIDTH=true

    if(
    $maxheight && $height $maxheight){ 
    $heightratio $maxheight/$height
    $RESIZEHEIGHT=true

    if(
    $RESIZEWIDTH && $RESIZEHEIGHT){ 
    if(
    $widthratio $heightratio){ 
    $ratio $widthratio
    }else{ 
    $ratio $heightratio

    }elseif(
    $RESIZEWIDTH){ 
    $ratio $widthratio
    }elseif(
    $RESIZEHEIGHT){ 
    $ratio $heightratio

    $newwidth $width $ratio
    $newheight $height $ratio
    if(
    function_exists("imagecopyresampled")){ 
    $newim imagecreatetruecolor($newwidth$newheight); 
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height); 
    }else{ 
    $newim imagecreate($newwidth$newheight); 
    imagecopyresized($newim$im0000$newwidth$newheight$width$height); 

    ImageJpeg ($newim$thumbdir $name ".jpg");
    ImageDestroy ($newim); 
    } else { 
    ImageJpeg ($im$thumbdir $name ".jpg"); 


    $filename $name



    if (
    $_FILES['image']['size']) 

    if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){ 
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']); 
    }elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){ 
    $im imagecreatefrompng($_FILES['image']['tmp_name']); 
    }elseif(
    $_FILES['image']['type'] == "image/gif"){ 
    $im imagecreatefromgif($_FILES['image']['tmp_name']); 


    if (
    $im

    if (
    file_exists("$thumbdir/{$_FILES['image']['tmp_name']}")) 

    unlink("$thumbdir/{$_FILES['image']['tmp_name']}"); 

    ResizeImage($im$RESIZEWIDTH$RESIZEHEIGHT$_FILES['image']['name']); 
    ImageDestroy ($im); 



    ?> 
    <html> 
    <head> 
    <title>Resize Thing</title> 
    </head> 

    <body> 

    <img src="/pic/<?=$filename?>.jpg"> 

    <form enctype="multipart/form-data" method="post"> 
    <b>Resize Image</b><br> 
    <input type="file" name="image" size="50"><br><br> 
    <input type="submit" value="Upload Photo"> 
    </form> 

    </body> 
    </html>
    :'(

  19. #19
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should be easy enough to fix:

    httpd.www/mp/pic/ = where the thumbs are actually going
    /pic/ = where your html img tag points to.

    If the script is in the mp directory then the img src should be pic/ (no front slash) otherwise try changing it for /mp/pic/ and see what happens.

    That literally is all it is!

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  20. #20
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Wow! Thanks a lot. Now it works.
    How do I get to be as good as you are doing this?
    For how long have you been playing around with php (or do you do this as a job as well?)
    I'm reading my 4th book on php/mySQL now, so I hope I will remember anything after all.

    Anyway. Thanx a lot for your help with this script.

  21. #21
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah PHP is my full-time job, have been using it for all my web development since it was called PHP/FI (so, quite a long time ago). Books are good though - just keep experimenting and playing around, I honestly believe you learn as much through trial and error as you do from seeing other peoples solutions.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.


Bookmarks

Posting Permissions

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