[FIXED]working with images

    Date: 02/21/06 (PHP Community)    Keywords: no keywords

    i've never had to work with images before, so this is my first time and i'm quite enjoying it, however i seem to be getting black images as opposed to resized version of the files i input, what am i doing wrong?


    class upload_utility
    {
    var $settings;
    var $presettings;

    public function __construct($settings)
    {
    global $presettings;

    $this->settings = $settings;
    $this->_POST = $_POST;
    $this->presettings = $presettings;
    //$this->_POST['imgfile'] = $_FILES['imgfile'];

    echo "1";
    }

    private function checkfilename()
    {
    $file_ext = strtolower(substr($_FILES['imgfile']['name'], -4));

    if (!in_array($file_ext, array('.jpg','jpeg','.gif','.png')))
    {
    unlink($this->_POST['imgfile']);
    eval(standard_error(fetch_error('grps_extension_unknown')));
    die;
    }

    echo "2";
    }

    private function makelargecopy()
    {
    //if (is_uploaded_file($_FILES['imgfile']))
    if (is_uploaded_file($_FILES['imgfile']['tmp_name']))
    {
    $this->imagedimes = getimagesize($_FILES['imgfile']['tmp_name']);
    $imagewidth = $this->imagedimes[0];
    $imageheight = $this->imagedimes[1];
    $maxheight = $this->settings['bigimgsize'];
    $imageog = $imageheight;
    $imgdimes = array();

    if ($imageheight > $maxheight)
    {
    $imageproportions = ($maxheight * 100) / $imageheight;
    $imgdimes['width'] = ($imagewidth * $imageproportions) / 100 ;
    $imgdimes['height'] = $maxheight;
    }

    if ($imageheight <= $maxheight)
    {
    $imageproportions = ($maxheight * 100) / $imageheight;
    $imgdimes['width'] = ($imagewidth * $imageproportions) / 100 ;
    $imgdimes['height'] = $maxheight;
    }

    $newimagebg = imagecreatetruecolor($imgdimes['width'],$imgdimes['height']);

    // Get Mime Type
    switch ($this->imagedimes['mime'])
    {
    case 'image/jpeg':
    $newimage = imagecreatefromjpeg($_FILES['imgfile']['tmp_name']);
    imagecopyresampled($newimagebg, $newimage, 0, 0, 0, 0, $$imgdimes['width'], $imgdimes['height'], $imagewidth, $imageheight);
    $newfilename = $this->settings['imgdir'] . "/" . intval($this->_POST['g']) . "_" . intval($this->_POST['u']) . "_" . TIMENOW . ".jpg";
    imagejpeg($newimagebg, $newfilename, $this->settings['bigimgquality']);
    break;
    case 'image/gif':
    $newimage = imagecreatefromgif($_FILES['imgfile']['tmp_name']);
    imagecopyresampled($newimagebg, $newimage, 0, 0, 0, 0, $$imgdimes['width'], $imgdimes['height'], $imagewidth, $imageheight);
    $newfilename = $this->settings['imgdir'] . "/" . intval($this->_POST['g']) . "_" . intval($this->_POST['u']) . "_" . TIMENOW . ".gif";
    imagegif($newimagebg, $newfilename, $this->settings['bigimgquality']);
    break;
    case 'image/png':
    $newimage = imagecreatefrompng($_FILES['imgfile']['tmp_name']);
    imagecopyresampled($newimagebg, $newimage, 0, 0, 0, 0, $$imgdimes['width'], $imgdimes['height'], $imagewidth, $imageheight);
    $newfilename = $this->settings['imgdir'] . "/" . intval($this->_POST['g']) . "_" . intval($this->_POST['u']) . "_" . TIMENOW . ".png";
    imagepng($newimagebg, $newfilename, $this->settings['bigimgquality']);
    break;
    default:
    eval(standard_error(fetch_error('grps_upload_error')));
    break;
    }
    }
    else
    {
    eval(standard_error(fetch_error('grps_upload_error_wtf')));
    }

    echo "3";
    }

    private function makethumbcopy()
    {
    if (is_uploaded_file($_FILES['imgfile']['tmp_name']))
    {
    $this->imagedimes = getimagesize($_FILES['imgfile']['tmp_name']);
    $imagewidth = $this->imagedimes[0];
    $imageheight = $this->imagedimes[1];
    $maxheight = $this->settings['thumbimgsize'];
    $imageog = $imageheight;
    $imgdimes = array();

    if ($imageheight > $maxheight)
    {
    $imageproportions = ($maxheight * 100) / $imageheight;
    $imgdimes['width'] = ($imagewidth * $imageproportions) / 100 ;
    $imgdimes['height'] = $maxheight;
    }

    if ($imageheight <= $maxheight)
    {
    $imageproportions = ($maxheight * 100) / $imageheight;
    $imgdimes['width'] = ($imagewidth * $imageproportions) / 100 ;
    $imgdimes['height'] = $maxheight;
    }

    $newimagebg = imagecreatetruecolor($imgdimes['width'],$imgdimes['height']);

    // Get Mime Type
    switch ($this->imagedimes['mime'])
    {
    case 'image/jpeg':
    $newimage = imagecreatefromjpeg($_FILES['imgfile']['tmp_name']);
    imagecopyresampled($newimagebg, $newimage, 0, 0, 0, 0, $$imgdimes['width'], $imgdimes['height'], $imagewidth, $imageheight);
    $newfilename = $this->settings['imgdir'] . "/" . intval($this->_POST['g']) . "_" . intval($this->_POST['u']) . "_" . TIMENOW . "_thumb.jpg";
    imagejpeg($newimagebg, $newfilename, $this->settings['thumbimgquality']);
    break;
    case 'image/gif':
    $newimage = imagecreatefromgif($_FILES['imgfile']['tmp_name']);
    imagecopyresampled($newimagebg, $newimage, 0, 0, 0, 0, $$imgdimes['width'], $imgdimes['height'], $imagewidth, $imageheight);
    $newfilename = $this->settings['imgdir'] . "/" . intval($this->_POST['g']) . "_" . intval($this->_POST['u']) . "_" . TIMENOW . "_thumb.gif";
    imagegif($newimagebg, $newfilename, $this->settings['thumbimgquality']);
    break;
    case 'image/png':
    $newimage = imagecreatefrompng($_FILES['imgfile']['tmp_name']);
    imagecopyresampled($newimagebg, $newimage, 0, 0, 0, 0, $$imgdimes['width'], $imgdimes['height'], $imagewidth, $imageheight);
    $newfilename = $this->settings['imgdir'] . "/" . intval($this->_POST['g']) . "_" . intval($this->_POST['u']) . "_" . TIMENOW . "_thumb.png";
    imagepng($newimagebg, $newfilename, $this->settings['thumbimgquality']);
    break;
    default:
    eval(standard_error(fetch_error('grps_upload_error')));
    break;
    }

    echo "4";
    }
    else
    {
    eval(standard_error(fetch_error('grps_upload_error')));
    }
    }

    private function cleanup()
    {
    imagedestroy($this->newimage);
    echo "5";
    }

    public function process()
    {
    //print_r($_POST);
    //print_r($_FILES);
    //die;
    $this->checkfilename();
    $this->makelargecopy();
    $this->makethumbcopy();
    $this->cleanup();

    echo "got here";
    }
    }


    there were some double $$'s in there.

    Source: http://community.livejournal.com/php/416624.html

« Web 2.0 || editing php.ini using... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home