Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Help with Saving Image to Folder instead or Browser

    Hi, I have been following a tutorial for image watermarking, and everything is working great. Only i cannot get the final image to save to a folder instead of show up in the browser as a file.

    I've searched everywhere but i cannot fathom how to in-corporate it with this code.

    Hope someone can put me out of my misery.

    thanks
    Lucy

    Code:
    <?php
        $photo = $_GET['photo'];
    	
    	
    	$src = imagecreatefromjpeg( "$photo" );
    	$dst = imagecreatefromjpeg( 'card.jpg' );
    	imagecopyresampled( $dst, $src, 43, 260, 0, 0, 355, 199, 355, 199 );
    	header( 'Content-type: image/jpeg' );
    	imagejpeg( $dst );
    	imagedestroy( $src );
    	imagedestroy( $dst );
    ?>
    
    <?
    /**
     * easy image resize function
     * @param  $file - file name to resize
     * @param  $width - new image width
     * @param  $height - new image height
     * @param  $proportional - keep image proportional, default is no
     * @param  $output - name of the new file (include path if needed)
     * @param  $delete_original - if true the original image will be deleted
     * @param  $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink
     * @param  $quality - enter 1-100 (100 is best quality) default is 100
     * @return boolean|resource
     */
      function smart_resize_image($file,
                                  $width              = 0, 
                                  $height             = 0, 
                                  $proportional       = false, 
                                  $output             = 'file', 
                                  $delete_original    = true, 
                                  $use_linux_commands = false,
      							  $quality = 10
      		 ) {
          
        if ( $height <= 0 && $width <= 0 ) return false;
    
        # Setting defaults and meta
        $info                         = getimagesize($file);
        $image                        = '';
        $final_width                  = 0;
        $final_height                 = 0;
        list($width_old, $height_old) = $info;
    
        # Calculating proportionality
        if ($proportional) {
          if      ($width  == 0)  $factor = $height/$height_old;
          elseif  ($height == 0)  $factor = $width/$width_old;
          else                    $factor = min( $width / $width_old, $height / $height_old );
    
          $final_width  = round( $width_old * $factor );
          $final_height = round( $height_old * $factor );
        }
        else {
          $final_width = ( $width <= 0 ) ? $width_old : $width;
          $final_height = ( $height <= 0 ) ? $height_old : $height;
        }
    
        # Loading image to memory according to type
        switch ( $info[2] ) {
          case IMAGETYPE_GIF:   $image = imagecreatefromgif($file);   break;
          case IMAGETYPE_JPEG:  $image = imagecreatefromjpeg($file);  break;
          case IMAGETYPE_PNG:   $image = imagecreatefrompng($file);   break;
          default: return false;
        }
        
        
        # This is the resizing/resampling/transparency-preserving magic
        $image_resized = imagecreatetruecolor( $final_width, $final_height );
        if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
          $transparency = imagecolortransparent($image);
    
          if ($transparency >= 0) {
            $transparent_color  = imagecolorsforindex($image, $trnprt_indx);
            $transparency       = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
            imagefill($image_resized, 0, 0, $transparency);
            imagecolortransparent($image_resized, $transparency);
          }
          elseif ($info[2] == IMAGETYPE_PNG) {
            imagealphablending($image_resized, false);
            $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
            imagefill($image_resized, 0, 0, $color);
            imagesavealpha($image_resized, true);
          }
        }
        imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
        
        # Taking care of original, if needed
        if ( $delete_original ) {
          if ( $use_linux_commands ) exec('rm '.$file);
          else @unlink($file);
        }
    
        # Preparing a method of providing result
        switch ( strtolower($output) ) {
          case 'browser':
            $mime = image_type_to_mime_type($info[2]);
            header("Content-type: $mime");
            $output = NULL;
          break;
          case 'file':
            $output = $file;
          break;
          case 'return':
            return $image_resized;
          break;
          default:
          break;
        }
        
        # Writing image according to type to the output destination and image quality
        switch ( $info[2] ) {
          case IMAGETYPE_GIF:   imagegif($image_resized, $output, $quality);    break;
          case IMAGETYPE_JPEG:  imagejpeg($image_resize, $output, $quality);   break;
    	                       // imagejpeg($image_resized, "photo/boom.jpg", 90); break;
          case IMAGETYPE_PNG:   imagepng($image_resized, $output, $quality);    break;
          default: return false;
        }
    
        return true;
      }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Provide a path to where you want to save the image including the file name to the imagejpeg function. The second argument is the path to save it to, and the third the quality.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mobimad (09-05-2013)


  •  

    Tags for this Thread

    Posting Permissions

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