PHP Classes

Class correction (partial image grab)

Recommend this page to a friend!

      picgrab  >  All threads  >  Class correction (partial image grab)  >  (Un) Subscribe thread alerts  
Subject:Class correction (partial image grab)
Summary:A correction to prevent partial image grabbing.
Messages:2
Author:Don van der Haghen
Date:2007-01-23 21:17:31
Update:2007-01-23 21:22:04
 

  1. Class correction (partial image grab)   Reply   Report abuse  
Picture of Don van der Haghen Don van der Haghen - 2007-01-23 21:17:31
There seem to be some buffering issues with the class, since the fread function isn't executed in a while() statement.
In my case the buffer only got filled till 1460 bytes in one fread(), so the image only partially got grabbed.

I have taken the liberty to modify the class to resolve this issue:
<?php
##########################################################################
# @author: elmar eigner #
# @contact: [email protected] #
# @revision: 1.1 #
# @release-date: 2002/06/27
# @modified: 2007/01/23 <[email protected]> #
# @classname: picgrab #
##########################################################################
class picgrab {
var $result = false;
var $orgfile;
var $newfile;
var $priverror;
var $showresult;

function picgrab($domain,$pfad,$neuername,$out) {
$this->orgfile = $domain.$pfad;
$this->showresult = $out;
$fp = fopen ("http://".$this->orgfile,"rb");
if (!$fp) {
$this->$priverror = "couln't open $this->orgfile";
return false;
} else {
$data = fread($fp,6000000);
fclose($fp);
}
if(preg_match("/\.([a-z]*)$/i",$this->orgfile,$filetypes)) {
$imgtype = $filetypes[1];
$this->newfile = $neuername.".".$imgtype;
$myNewImage = fopen($this->newfile,"wb");
if($myNewImage) {
fputs($myNewImage,$data);
fclose($myNewImage);
$this->result = true;
}
else {
$this->$priverror = "couln't save $this->orgfile as $this->newfile!";
return false;
}
}
if($this->result) {
if($this->showresult==1) echo "succesfully saved $this->orgfile as <a href='$this->newfile' target='_blank'>$this->newfile</a>!";
}
else {
if($this->showresult==1) echo $this->$priverror;
}
}
} // end class picgrab
##########################################################################
# @Usage-Sample of class picgrab #
# @PARAMS: #
# domain (without leading http!) #
# path to picture #
# name (path+name) to save as #
# result flag (0=don't show, 1=show) #
##########################################################################
$p1 = new picgrab("www.nichtohne.de","/oe/pix/head_3.jpg","open-eye",1);
?>

  2. Re: Class correction (partial image grab)   Reply   Report abuse  
Picture of Don van der Haghen Don van der Haghen - 2007-01-23 21:22:04 - In reply to message 1 from Don van der Haghen
Sorry! Previous message contains the old class (copy/paste mistake).

There is the correction version:
<?php
##########################################################################
# @author: elmar eigner #
# @contact: [email protected] #
# @revision: 1.2 #
# @release-date: 2002/06/27
# @modified: 2007/01/23 <[email protected]> #
# @classname: picgrab #
##########################################################################
class picgrab {
var $result = false;
var $orgfile;
var $newfile;
var $priverror;
var $showresult;

function picgrab($domain,$pfad,$neuername,$out) {
$this->orgfile = $domain.$pfad;
$this->showresult = $out;
$fp = fopen ("http://".$this->orgfile,"rb");
if (!$fp) {
$this->$priverror = "couln't open $this->orgfile";
return false;
}
if(preg_match("/\.([a-z]*)$/i",$this->orgfile,$filetypes)) {
$imgtype = $filetypes[1];
$this->newfile = $neuername.".".$imgtype;
$myNewImage = fopen($this->newfile,"wb");
if($myNewImage) {
while ($data = fread($fp,1024000))
{
fputs($myNewImage,$data);
}
fclose($myNewImage);
fclose($fp);
$this->result = true;
}
else {
$this->$priverror = "couln't save $this->orgfile as $this->newfile!";
return false;
}
}
if($this->result) {
if($this->showresult==1) echo "succesfully saved $this->orgfile as <a href='$this->newfile' target='_blank'>$this->newfile</a>!";
}
else {
if($this->showresult==1) echo $this->$priverror;
}
}
} // end class picgrab