Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resize a jpeg without GD or imagemagick

Status
Not open for further replies.

BB101

Programmer
May 23, 2001
337
GB
Im doing some work for some people who have hosted web space. They want a the ability to resize jpegs, but unfortunatly their host doesn't support GD or Imagemagick.

Anyone?

--BB
 
I believe they are slightly stuck then, maybe they should consider upgrading?
 
I used imglib in a similar stituation, heres an index for finding appropriate files and imglib.php (note copyrights etc ), monkey as required.
------------------------------------------------------------
index.php
---------
<?
include(&quot;imglib.php&quot;);
// the page header
include(&quot;header.inc&quot;);


/////////////////
// config variable
$col = 4; // number of column
$maxrow = 4; // max row for table
$dir = &quot;./images&quot;; // the directory where your images are
////////////////////////

// open the directory
$dh = opendir($dir);
$i=0;
while($file = readdir($dh))
{
if( (substr($file,-3)==&quot;gif&quot;) || (substr($file,-3)==&quot;jpg&quot;) )
{
// insert all images in array
$a_img[] = $file;
$i++;
}
}

$totimg = $i; // total images number
$totxpage = $col*$maxrow; // images x page
$totpages = ($totimg%$totxpage==0)?((int)$totimg/$totxpage):((int)($totimg/$totxpage)+1); // number of total pages

//////////////////////////////////////////////////////////////////
//
// Start to print and page break
//
//////////////////////////////////////////////////////////////////

print &quot;<center><table width=700 bgcolor=#ffcc33 border=1 bordercolor=#000000>\n&quot;;
// start page
if($page==&quot;&quot; || $page==1)
{
$x=0;
$page = 1;
}
else
$x = (($page-1)*($totxpage));
$r=0;

// print of table
foreach($a_img as $key=>$val)
{
if(($x%$col)==0)
print &quot;<tr>\n&quot;;
if($a_img[$x])
{
// create thumbnail with imglib
$hg = (getimgheight(&quot;$dir/$a_img[$x]&quot;));
$height = 120;
$width = (int)((getimgwidth(&quot;$dir/$a_img[$x]&quot;))*120/$hg);
/////////
$ctime = filectime(&quot;$dir/$a_img[$x]&quot;);
$sizes=getimagesize(&quot;$dir/$a_img[$x]&quot;);
$xx=($sizes[0] + 15);
$yy=($sizes[1] + 15 );
//print_r($sizes).&quot;<br>&quot;;
print &quot;<td align=center>
<a href=\&quot;#\&quot; onclick='window.open(\&quot;$dir/$a_img[$x]\&quot;, \&quot;popup\&quot;,\&quot;width=$xx,height=$yy,scrollbars=no,menubar=no,resizable=no \&quot;);'
><img src=\&quot;$dir/$a_img[$x]\&quot; height=$height width=$width border=0></a>
<br>$dir/$a_img[$x]<br>&quot;.date(&quot;d/m/Y&quot;,$ctime).&quot;</td>\n&quot;;



/*
print &quot;
<td align=center>
<a href=\&quot;$dir/$a_img[$x]\&quot; target=\&quot;_new\&quot;>
<img src=\&quot;$dir/$a_img[$x]\&quot; height=$height width=$width border=0></a>
<br>$dir/$a_img[$x]<br>&quot;.date(&quot;d/m/Y&quot;,$ctime).&quot;</td>\n
&quot;;

*/
}
if(($x%$col) == ($col-1))
{
print &quot;</tr>\n&quot;;
$r++;
}
if($r==$maxrow)
{
print &quot;</table>\n&quot;;
break;
}
else
$x++;
}

// page break
print &quot;<br><hr width=780 color=#0000FF><font size=2 face=verdana>&quot;;
if($totimg>$totxpage)
{
if($totpages>$page)
{
$next = $page+1;
$back = ($page>1)?($page-1):&quot;1&quot;;
if($page>1)
{
$back = $page-1;
print &quot;<a href=index.php?page=$back><< back </a>&quot;;
}
print &quot; &nbsp;&nbsp; <b>gallery $page of $totpages</b> &nbsp;&nbsp;<a href=index.php?page=$next>next >></a>&quot;;
}
else
{
$next = (($page-1)==0)?&quot;1&quot;:($page-1);
print &quot;<a href=index.php?page=$next><< back</a>&nbsp;&nbsp; <b>gallery $page of $totpages</b> &nbsp;&nbsp;&quot;;

}
}
///
//////////////////////////////////////////////////////////////////////////////////////////////////

// your footer page
include(&quot;footer.inc&quot;);
?>


------------------------------------------------------------
imglib.php
----------
<?
// IMAGE LIBRARY 1.0 FOR PHP WITHOUT USING GDI
// THIS SCRIPT IS MADE BY SLIM WEB ARCHITECTURE
//
// This script is made with help from a perl based script of origin unknown
// It is intended for those whose webhost does not have the GD library installed
// If anyone knows how to read and or write JPG, GIF or PNG feel free to add it to this script

// The nice thing about PHP is its possiblities. The bad thing is that virtually no provider has its full capabilities installed

function getimgwidth($filename) {
global $imgw,$imgh;
$ext=strtoupper(substr($filename,-3));
switch ($ext) {
case &quot;GIF&quot;:readgif($filename);return $imgw;break;
case &quot;PNG&quot;:readpng($filename);return $imgw;break;
case &quot;JPG&quot;:readjpg($filename);return $imgw;break;
case &quot;PEG&quot;:readjpg($filename);return $imgw;break;
default: return &quot;0&quot;;
}
}

function getimgheight($filename) {
global $imgw,$imgh;
$ext=strtoupper(substr($filename,-3));
switch ($ext) {
case &quot;GIF&quot;:readgif($filename);return $imgh;break;
case &quot;PNG&quot;:readpng($filename);return $imgh;break;
case &quot;JPG&quot;:readjpg($filename);return $imgh;break;
case &quot;PEG&quot;:readjpg($filename);return $imgh;break;
default: return &quot;0&quot;;
}
}

function readpng($imgfn) {
global $imgw,$imgh;
$imgfd = fopen ($imgfn, &quot;r&quot;);
$imgheader=fread ($imgfd, 8);
if ($imgheader!=chr(211).&quot;PNG\r\n&quot;.chr(32).&quot;\n&quot; && $imgheader!=chr(137).&quot;PNG&quot;.chr(13).chr(10).chr(26).chr(10)) {
fclose($imgfd);
// return &quot;not a PNG file: *&quot;.ord($imgheader[0]).&quot;*&quot;.ord($imgheader[1]).&quot;*&quot;.ord($imgheader[2]).&quot;*&quot;.ord($imgheader[3]).&quot;*&quot;.ord($imgheader[4]).&quot;*&quot;.ord($imgheader[5]).&quot;*&quot;.ord($imgheader[6]).&quot;*&quot;.ord($imgheader[7]).&quot;*&quot;);
return &quot;0&quot;;
}
$imgheader=fread ($imgfd, 8);
$imgheader=unpack(&quot;Nw&quot;,fread ($imgfd, 4));
$imgw=$imgheader[&quot;w&quot;];
$imgheader=unpack(&quot;Nh&quot;,fread ($imgfd, 4));
$imgh=$imgheader[&quot;h&quot;];
fclose($imgfd);
return &quot;width=$imgw height=$imgh&quot;;
}

function readjpg($imgfn) {
global $imgw,$imgh;
$M_SOF0 = chr(192);
$M_SOF15 = chr(207);
$M_SOI = chr(216);
$M_EOI = chr(217);
$M_SOS = chr(218);
$M_COM = chr(254);
$MARKER = chr(255);
$imgfd = fopen ($imgfn, &quot;r&quot;);
$imgheader=fread ($imgfd, 2);
if ($imgheader[0]!=$MARKER || $imgheader[1]!=$M_SOI) {
fclose($imgfd);
// die(&quot;not a JPG file&quot;);
return &quot;0&quot;;
}
while (1) {
$db=0;
$c1=fread ($imgfd, 1);
while ($c1 != $MARKER) {
$db++;
$c1 = fread ($imgfd, 1);
}
while ($c1 == $MARKER) {
$c1 = fread ($imgfd, 1);
}
// if ($db) {
// echo &quot;Warning: garbage data found in JPG file \&quot;$imgfn\&quot;\n&quot;;
// }
if ($c1 >= $M_SOF0 && $c1 <= $M_SOF15) {
// Do we have width and height?
$imgheader = unpack(&quot;nl/Cd/nh/nw&quot;, fread ($imgfd, 7));
$l = $imgheader[&quot;l&quot;];
$d = $imgheader[&quot;d&quot;];
$imgh = $imgheader[&quot;h&quot;];
$imgw = $imgheader[&quot;w&quot;];
fclose($imgfd);
return &quot;width=$imgw height=$imgh&quot;;
} elseif ($c1 == $M_SOS || $c1 == $M_EOI) {
// Did we reach header end?
fclose($imgfd);
// die(&quot;endoffile&quot;);
return &quot;0&quot;;
} else {
// Otherwise, skip this variable
$junk=unpack(&quot;ndummy&quot;, fread ($imgfd, 2));
$l = ushort($junk[&quot;dummy&quot;]) - 2;
if ($l < 0) {
fclose($imgfd);
// die(&quot;errormarker&quot;);
return &quot;0&quot;;
}
fread ($imgfd, $l);
}
}
}

function readgif($imgfn) {
global $imgw,$imgh;
$imgfd = fopen ($imgfn, &quot;r&quot;);
$imgheader=fread ($imgfd, 6);
if ($imgheader != &quot;GIF87a&quot; && $imgheader != &quot;GIF89a&quot;) {
fclose($imgfd);
// die(&quot;$imgfn is not a GIF file&quot;);
return &quot;0&quot;;
}
// Examine the Logical Screen Descriptor
// my($lsw, $lsh, $pf, $bg, $par) = unpack(&quot;vvCCC&quot;, read_n_bytes(7));
$imgheader = unpack(&quot;vnul/veen/Ctwee/Cdrie/Cvier&quot;, fread ($imgfd, 7));
$pf = $imgheader[&quot;twee&quot;];
// Is it followed by a Global Color table?
if ($pf & 128) {
// Skip the Global Color Table
$GCTsize = $pf & 7;
fread ($imgfd, 3 * (2 << $GCTsize));
}
// Go through the markers in the header. Stop when height and width are
// determined or when end of header is reached
while (1) {
// Get the next marker
$c = fread ($imgfd, 1);
if ($c == chr(33)) {
// This is an Extension.
// Read the label.
$c = fread ($imgfd, 1);
// Read the remainder of this Extension Block and while we're at it,
// read all possible Data Sub-blocks as well.
while ($blksize = ord(fread ($imgfd, 1))) {
fread ($imgfd, $blksize);
}
} elseif ($c == chr(44)) {
// This is the most holy of all... The Image Descriptor.
$nheader=unpack(&quot;vnul/veen/vtwee/vdrie/Cvier&quot;, fread ($imgfd, 9));
$imgw=ushort($nheader[&quot;twee&quot;]);
$imgh=ushort($nheader[&quot;drie&quot;]);
fclose($imgfd);
return &quot;width=$imgw height=$imgh&quot;;
} else {
fclose($imgfd);
return &quot;nothing&quot;;
}
}
}

function ushort($n) {
if ($n < 0) {$n += 65536;}
return $n;
}

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
and if u rnt bothered bout clarity u could use the HTML <img> tag's height and width property...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top