Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to crop images in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 282
    Comment on it

    Hello Reader's if you want to crop the images and save them then you can use this code liberay, its a very easy to implement and very useful for thumbnail generation

    <?php
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;
    
    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
    
    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);
    
    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);
    ?>
    
    <?php
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;
    
    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
    
    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);
    
    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);
    ?>
    

    Now put the header like this:-

    //header('Content-type: image/jpeg');
    imagejpeg($dst_r,PATH_TO_SAVE_IMAGE,$jpeg_quality);
    

    Javascript Configurations: And the script will go like this

    <script type="text/javascript">
        $(function(){
        $(\'#cropbox\').Jcrop({
        aspectRatio: 1,
        onSelect: updateCoords,
    //    minSize:[200,200], start minimum image size
    //    maxSize:[200,200], max size should be...
        });
    });
    </script>
    
    
    
    
    <script type="text/javascript">
        $(function(){
        $(\'#cropbox\').Jcrop({
        aspectRatio: 1,
        onSelect: updateCoords,
    //    minSize:[200,200], start minimum image size
    //    maxSize:[200,200], max size should be...
        });
    });
    </script>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: