Jump to content

Image compression without GD library?


Topsy Turvey

Recommended Posts

Hi,

 

I know this is fairly easy with the GD library but what would be the easiest way to resample (not just resize) an image upload to a set pixel width before

copying it to its new directory? The reason I want to do this is so the image does not appear greater than the screen width when viewed in a "JS Lightbox"

javascript window without having to 'squash' it by adjusting the width / height in my gallery markup...

 

Any help would relieve me of acute confusion and annoyance!  :P

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/57834-image-compression-without-gd-library/
Share on other sites

<?        // Resizer.php

// I wrote this function so that I could display thumbnails of pictures users
// uploaded without having to actually modify the file. This function will return
// a height and width to put in your <IMG> tag that will be proportionate to each 
// other. Again, the actual file is not modified, and GD is NOT required. Modifying
// this to your needs should be pretty simple, but this will give you an idea on 
// how to do it.


$pic = "image.jpg";                        // This is the name of the image to modify
$dimensions = resize($pic);                 // Here we call our function to get new 
height/width for the image.

// Print it all our
echo "<HTML>\n<BODY>\n";
echo "<img src=\"$pic\" width=\"$dimensions[0]\" height=\"$dimensions[1]\"/>\n";
echo "</BODY>\n</HTML>";

function resize($v_pic) {                // This is our main function

$largestside = 120;                        // This is the size of the LARGEST SIZE we 
want either our width or height to be
$size = GetImageSize ("$v_pic");        // Get the actual width/height of the 
image...

$width = $size[0];
$height = $size[1];

if ($width == $height) {                // If the height == width
$dimensions[0] = $largestside;        // Assign both width and height the value of 
$largestsize
$dimensions[1] = $largestside;
} elseif ($width > $height) {                // If the width is greater than the 
height
$divisor = $width / $largestside;
$height = $height / $divisor;
$dimensions[0] = $largestsize;        // Assign $largestsize to width
$dimensions[1] = intval ($height);        // and assign $height a 
proportionate value to $height
} elseif ($width < $height) {                // If width is less than height
$divisor = $height / 120;
$width = $width / $divisor;
$dimensions[0] = intval ($width);        // Set width to be proportionate to height
$dimensions[1] = 120;                // Set height to be $largestsize
}

return $dimensions;
}
?> 

 

litle bar graph for u as well ok.

<html> 
<head> 
<meta http-equiv="Content-Language" content="en-us"> 
<title>Simple Bar Graph .. by viji patil</title> 
</head> 
<body> 
<?php 
    
        // read the post data 
        $data = array('100','200','300','400','500','350','270'); 
        $x_fld = array('Sun','Mon','Tue','Wen','Thu','Fir','Sat'); 
        $max = 0; 
        for ($i=0;$i<7;$i++){ 
          if ($data[$i] > $max)$max=$data[$i];  // find the largest data 
        } 
        $im = imagecreate(320,255); // width , height px 

        $white = imagecolorallocate($im,255,255,255); // allocate some color from RGB components remeber Physics 
        $black = imagecolorallocate($im,0,0,0);   // 
        $red = imagecolorallocate($im,255,0,0);   // 
        $green = imagecolorallocate($im,0,255,0); // 
        $blue = imagecolorallocate($im,0,0,255);  // 
        // 
        // create background box 
        //imagerectangle($im, 1, 1, 319, 239, $black); 
        //draw X, Y Co-Ordinate 
        imageline($im, 10, 5, 10, 230, $blue ); 
        imageline($im, 10, 230, 300, 230, $blue ); 
        //Print X, Y 
        imagestring($im,3,15,5,"Students",$black); 
        imagestring($im,3,280,240,"Days",$black); 
        imagestring($im,5,100,50,"Simple Graph",$red); 
         imagestring($im,5,125,75,"by Vijit",$green); 

        // what next draw the bars 
        $x = 15;    // bar x1 position 
        $y = 230;    // bar $y1 position 
        $x_width = 20;  // width of bars 
        $y_ht = 0; // height of bars, will be calculated later 
        // get into some meat now, cheese for vegetarians; 
        for ($i=0;$i<7;$i++){ 
          $y_ht = ($data[$i]/$max)* 100;    // no validation so check if $max = 0 later; 
          imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red); 
          imagestring( $im,2,$x-1,$y+1,$x_fld[$i],$black); 
          imagestring( $im,2,$x-1,$y+10,$data[$i],$black); 
          $x += ($x_width+20);   // 20 is diff between two bars; 
          
        } 
        imagejpeg( $im, "graph.jpeg", 90); 
        imagedestroy($im); 
        echo "<img src='graph.jpeg'><p></p>"; 
  
?> 
</body> 
</html>

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.