SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Saving Full Merged Image

    I am trying to save the entire merged image but so far I can only manage to get the code below to save the uploaded image. Any help would be greatly appreciated.

    PHP Code:
    <?php

    include("config.php");

    // Make sure an image is uploaded //
    if ($uploadfile == ""){ die("please upload a pic"); }

     
    /* TEST File Checking */  
    if($HTTP_POST_FILES['uploadfile']['size'] > 10000000000000000){
    die(
    "Image To Big!");
    }

    // Image extension check
    $file_name $HTTP_POST_FILES['uploadfile']['name']; 
    $file_ext strtolower(substr($file_namestrrpos($file_name".")));

    if (
    $file_ext != ".jpeg" AND $file_ext != ".jpg") { 
         die(
    "Please use only JPG or JPEG files.");
    }

    // This is the temporary file created by PHP 
    $uploadedfile $HTTP_POST_FILES['uploadfile']['tmp_name'];

    // Create an Image from it so we can do the resize
    $src imagecreatefromjpeg($uploadedfile);

    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);

    $newwidth=156;
    $newheight=84;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    // this line actually does the image resizing, copying from the original
    // image into the $tmp image
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $filename "photos/"$HTTP_POST_FILES['uploadfile']['name'];
    imagejpeg($tmp,$filename,100);

    // Create image instances
    $dest imagecreatefromjpeg('image.jpg');
    $src imagecreatefromjpeg("photos/".$HTTP_POST_FILES['uploadfile']['name']);

    // Copy and merge
    imagecopymerge($dest$src2361480015684100);

    // Rename Image here
    $newname rand(1,9999999);
    rename("photos/".$HTTP_POST_FILES['uploadfile']['name'], "photos/$newname$file_ext");

    // Output and free free from memory
    //header("Content-Disposition: attachment; filename=image$file_ext");
    //header("Pragma: no-cache");
    //header('Content-Type: image/jpeg');
    //imagejpeg($dest);

    //imagedestroy($dest);
    //imagedestroy($src);

    // Remove the uploaded Pic from the server
    //unlink("photos/".$HTTP_POST_FILES['uploadfile']['name']);

    ?>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    imagejpeg() is the function which saves the image to the filesystem. The second argument is the filename you wish to save it as.

    You should do all image manipulation, and then save it with the desired filename.

  3. #3
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cwb View Post
    I am trying to save the entire merged image but so far I can only manage to get the code below to save the uploaded image. Any help would be greatly appreciated.
    You are not saving the merged file anywhere...
    imagecopymerge(); doesn't save the newly merged image.
    You have to do a imagejpeg(); of the merged file.

    I also advice you to read this page:
    http://pl.php.net/manual/en/reserved...bles.files.php

    and use $_FILES instead of $HTTP_POST_FILES.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  4. #4
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    You are not saving the merged file anywhere...
    imagecopymerge(); doesn't save the newly merged image.
    You have to do a imagejpeg(); of the merged file.

    I also advice you to read this page:
    http://pl.php.net/manual/en/reserved...bles.files.php

    and use $_FILES instead of $HTTP_POST_FILES.
    I am using the imagejpeg() function...
    PHP Code:
    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $filename "photos/"$HTTP_POST_FILES['uploadfile']['name'];
    imagejpeg($tmp,$filename,100); 
    And what is it with people telling me to use $_FILES and $HTTP_POST_FILES? This is the 3rd time someone has told me to use either or the other... lol

  5. #5
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cwb View Post
    I am using the imagejpeg() function...
    PHP Code:
    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $filename "photos/"$HTTP_POST_FILES['uploadfile']['name'];
    imagejpeg($tmp,$filename,100); 
    And what is it with people telling me to use $_FILES and $HTTP_POST_FILES? This is the 3rd time someone has told me to use either or the other... lol
    In the script you posted you use the imagejpeg function to save the original uploaded file.

    after you do a merge you need to call that function again. Look at it this way: When you type some text for instance you save your file, but after editing the text you need to save it again, or the file won't contain the edited text. If you know what I mean.

    about the $_FILES array. Unless you are using PHP 4 or below (which is really old) it is better to use $_FILES. As you see in the manual, $HTTP_POST_FILES is depricated, which means it is outdated.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  6. #6
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your time fristi.
    I am a little lost at calling the function again after the merge function.
    Here is what I'm thinking but I know I am far off...

    PHP Code:
    // Copy and merge
    imagecopymerge($dest$src2361480015684100);

    // SAVE MERGED IMAGE HERE
    imagejpeg($tmp,photos/imagename.jpg,100); 

  7. #7
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cwb View Post
    Thank you for your time fristi.
    I am a little lost at calling the function again after the merge function.
    Here is what I'm thinking but I know I am far off...

    PHP Code:
    // Copy and merge
    imagecopymerge($dest$src2361480015684100);

    // SAVE MERGED IMAGE HERE
    imagejpeg($tmp,photos/imagename.jpg,100); 
    It is my pleasure


    Well you are almost right. Looking at the imagecopymerge function, you are merging a part of $src into $dest. So $dest is the one that is containing the merged image, therefore:


    PHP Code:
    // Copy and merge
    imagecopymerge($dest$src2361480015684100);

    // SAVE MERGED IMAGE HERE
    imagejpeg($dest$filename,100); 
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  8. #8
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did the trick...
    Thanks again for all your help.

  9. #9
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cwb View Post
    That did the trick...
    Thanks again for all your help.
    You are welcome, you are the first person I helped on these forums
    And the appreciation is a reward on itself
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  10. #10
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad I could be your first one


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
  •