Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:42:34+00:00 2026-05-31T17:42:34+00:00

I am compressing an image using following class.. <?php class SimpleImage { var $image;

  • 0

I am compressing an image using following class..

<?php
class SimpleImage {

var $image;
var $image_type;

function load($filename) {

  $image_info = getimagesize($filename);
  $this->image_type = $image_info[2];
  if( $this->image_type == IMAGETYPE_JPEG ) {

     $this->image = imagecreatefromjpeg($filename);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {

     $this->image = imagecreatefromgif($filename);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {

     $this->image = imagecreatefrompng($filename);
  }
 }
 function save($filename,$image_type=IMAGETYPE_JPEG,$compression=75,$permissions=null){

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image,$filename,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image,$filename);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image,$filename);
  }
  if( $permissions != null) {

     chmod($filename,$permissions);
  }
}
function output($image_type=IMAGETYPE_JPEG) {

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image);
  }
}
function getWidth() {

  return imagesx($this->image);
}
 function getHeight() {

  return imagesy($this->image);
}
function resizeToHeight($height) {

  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
}
function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
}
function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100;
  $this->resize($width,$height);
}
function resize($width,$height) {
  $new_image = imagecreatetruecolor($width, $height);
   imagecopyresampled($new_image,$this->image,0,0,0,0,$width,$height,$this->getWidth(),$this->getHeight());
  $this->image = $new_image;
}      

}

?>

And my php code looks like..

include_once(ROOT.'/library/SimpleImage.php')
$image = new SimpleImage();
$image->load($image_name);
$image->resizeToWidth(100);
$image->save($image_name);

Now the problem is that my images are getting compressed, but their original color combination get lost…
Don’t know what to do..??

Please suggest another class for image compression.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:42:35+00:00Added an answer on May 31, 2026 at 5:42 pm

    This is a function I coded a long time ago, it’s not realy optimized but it should do the trick. Works for various mime types and allows you to save the modified image into a new location. You can easily simplify it if you need less features.

    public function resizePicture($path, $new_path, $new_width, $new_height, $proportion = false) {
        $size = getimagesize($path);
        $x = 0;
        $y = 0;
    
        switch($size['mime']) {
            case 'image/jpeg':
                $picture = imagecreatefromjpeg($path);
            break;
            case 'image/png':
                $picture = imagecreatefrompng($path);
            break;
            case 'image/gif':
                $picture = imagecreatefromgif($path);
            break;
            default:
                return false;
            break;
        }
    
        $width = $size[0];
        $height = $size[1];
    
        $frame = imagecreatetruecolor($new_width, $new_height);
        if($size['mime'] == 'image/jpeg') {
            $bg = imagecolorallocate($frame, 255, 255, 255);
            imagefill($frame, 0, 0, $bg);
        } else if($size['mime'] == 'image/gif' or $size['mime'] == 'image/png') {
            imagealphablending($picture, false);
            imagesavealpha($picture, true);
            imagealphablending($frame, false);
            imagesavealpha($frame, true);
        }
    
        if($width < $new_width and $height < $new_height) {
            $x = ($new_width - $width) / 2;
            $y = ($new_height - $height) / 2;
            imagecopy($frame, $picture, $x, $y, 0, 0, $width, $height);
        } else {
            if($proportion and $width != $height) {
                if($width > $height) {
                    $old_height = $new_height;
                    $new_height = $height * $new_width / $width;
                    $y = abs($old_height - $new_height) / 2;
                } else {
                    $old_width = $new_width;
                    $new_width = $width * $new_height / $height;
                    $x = abs($old_width - $new_width) / 2;
                }
            }
            imagecopyresampled($frame, $picture, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
        }
    
        switch($size['mime']) {
            case 'image/jpeg':
                imagejpeg($frame, $new_path, 85);
            break;
            case 'image/png':
                imagepng($frame, $new_path, 8);
            break;
            case 'image/gif':
                imagegif($frame, $new_path);
            break;
            default:
                return false;
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using php's imagick to convert some PDF to images - This works
I'm compressing raw AVI files to WMV using the ASF Writer. I'm in a
I have an application where I am taking a bitmap and compressing it using
I'm using tumblr. I've inserted an image, html looks like: <img height=320 src=foo.png width=400
I'm using a solution for assembling image files to a zip and streaming it
I am trying to create TIFF image using Libtiff. I could not figure out
I am using UIImagePickerController for bring images from the photo library, i found this
I have an array that is taken from an image using exif_read_data($image, 0, true)
I am using S3 to store images and I am resizing and compressing images
I tried this experiment with Digital Image Processing - 2D DCT/IDCT ( image compression

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.