IT Community - Software Programming, Web Development and Technical Support

Image Compression. Save images with any quality..

This is a discussion on Image Compression. Save images with any quality.. within the PHP Programming forums, part of the Web Development category; JPEG Compression --> save JPEG images with any quality Using this code you can save an image (System.Drawing.Image) ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 02:28 AM
Mramesh Mramesh is offline
D-Web Sr.Programmer
 
Join Date: Sep 2007
Location: Chennai
Posts: 106
Mramesh is on a distinguished road
Send a message via MSN to Mramesh
Default Image Compression. Save images with any quality..

JPEG Compression --> save JPEG images with any quality


Using this code you can save an image (System.Drawing.Image) item as a jpeg image, specifying the quality of it. Jpeg quality is the same as compression so the lower the quality, the smaller the file size. It's simple to use, ie just do this:

Code:
Image myImage = //... load the image somehow 
// Save the image with a quality of 50% 
SaveJpeg (destImagePath, myImage, 50);
[CODE
//add this!
using System.Drawing.Imaging;

/// <summary>
/// Saves an image as a jpeg image, with the given quality
/// </summary>
/// <param name="path">Path to which the image would be saved.</param>
// <param name="quality">An integer from 0 to 100, with 100 being the
/// highest quality</param>
public static void SaveJpeg (string path, Image img, int quality)
{
if (quality<0 || quality>100)
throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");


// Encoder parameter for image quality
EncoderParameter qualityParam =
new EncoderParameter (Encoder.Quality, quality);
// Jpeg image codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;

img.Save (path, jpegCodec, encoderParams);
}

/// <summary>
/// Returns the image codec with the given mime type
/// </summary>
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec
for(int i=0; i<codecs.Length; i++)
if(codecs[i].MimeType == mimeType)
return codecs[i];
return null;
} [/code]

a small problem is though, that you should use long as quality, not int. Anyway that's the only way I got it to work, got an error the way you used it...
For EncoderParameter qualityParam = new EncoderParameter (Encoder.Quality, quality); quality can be something like 50L, or casting: (long)50, or you could use (long)quality, should also work... for me that does the trick anyway
__________________
M.Ramesh Kumar

Last edited by Mramesh : 02-22-2008 at 02:49 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-26-2008, 10:18 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Image Compression. Save images with any quality..

Hi,
i think this code in ASP.net, is there any script available from PHP?

It seems if we install GD or any other image library, we can do any image operation in PHP.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-11-2008, 01:16 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Image Compression. Save images with any quality..

The script is used for image compression such as photo gallery and the script automatically compress the image base on the given dimension. it will automatically calculate the landscape image or portrait image

before use this code you must have to enable your gd library




PHP Code:
$imgfile="photo/myson.jpg"// The Source of your image file
function imageCompression($imgfile="",$thumbsize=0,$savePath=NULL) {
    if(
$savePath==NULL) {
        
header('Content-type: image/jpeg');
        
/* To display the image in browser
       
        */
       
    
}
    list(
$width,$height)=getimagesize($imgfile);
    
/* The width and the height of the image also the getimagesize retrieve other information as well   */
    
echo $width;
    echo 
$height;
    
$imgratio=$width/$height
    
/*
    To compress the image we will calculate the ration 
    For eg. if the image width=700 and the height = 921 then the ration is 0.77...
    if means the image must be compression from its height and the width is based on its height
    so the newheight = thumbsize and the newwidth is thumbsize*0.77...
    */
   
    
if($imgratio>1) {
        
$newwidth=$thumbsize;
        
$newheight=$thumbsize/$imgratio;
    } else {
        
$newheight=$thumbsize;       
        
$newwidth=$thumbsize*$imgratio;
    }
   
    
$thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image
    
$source=imagecreatefromjpeg($imgfile); // Now it will create a new image from the source
    
imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);  // Copy and resize the image
    
imagejpeg($thumb,$savePath,100);
    
/*
    Out put of image 
    if the $savePath is null then it will display the image in the browser
    */
    
imagedestroy($thumb);
    
/*
        Destroy the image
    */
   
}
imageCompression($imgfile,150,"a.jpg"); 
i think it can help you
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Image Compression. Save images with any quality.. Mramesh C# Programming 2 02-25-2008 07:33 AM
Image Compression. Save images with any quality.. Mramesh VB.NET Programming 0 02-22-2008 02:43 AM
how do I save images to SQL DB? H2o ASP and ASP.NET Programming 2 09-19-2007 12:00 AM
Select Thumbnail images and view the larger image H2o HTML, CSS and Javascript Coding Techniques 1 08-07-2007 08:58 AM
Align images relative to a particular image? oyu2o HTML, CSS and Javascript Coding Techniques 0 03-10-2007 10:30 PM


All times are GMT -7. The time now is 02:31 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0