<?php

Class Labelimage {

    function setLabelImage($filename = '', $topleft = '', $topright = '', $bottomleft = '', $bottomright = '', $http = '', $imagefield, $productid, $width, $height, $scale = 50) {
        $orgfilename = $filename;
        $info = pathinfo($imagefield);
        $extension = $info['extension'];
        if (file_exists(DIR_IMAGE . substr($filename, strlen($http)))) {
            $filename = substr($filename, strlen($http));
        } else {
            return $filename;
        }
        if (empty($topleft) && empty($topright) && empty($bottomleft) && empty($bottomright))
            return $orgfilename;

        $info = pathinfo(DIR_IMAGE . $filename);
        $extension = $info['extension'];
        $original = new Image(DIR_IMAGE .$filename);
        //$original->resize($width, $height);
        //$original->save (DIR_IMAGE . 'cache/'.$filename);
        if (!empty($topleft) && file_exists(DIR_IMAGE . $topleft)) {
            $image = new Image(DIR_IMAGE . $topleft);
            $image->resize($width * ($scale / 100), $height * ($scale / 100));
            $this->imagecopymerge_alpha($original->image, $image->image, 0, 0, 0, 0, imagesx($image->image), imagesy($image->image), 100);
            imagesavealpha($original->image, true);
            unset($image);
        }
        if (!empty($topright) && file_exists(DIR_IMAGE . $topright)) {
            $image = new Image(DIR_IMAGE . $topright);
            $image->resize($width * ($scale / 100), $height * ($scale / 100));
            $this->imagecopymerge_alpha($original->image, $image->image, imagesx($original->image) - imagesx($image->image), 0, 0, 0, imagesx($image->image), imagesy($image->image), 100);
            imagesavealpha($original->image, true);
            unset($image);
        }

        if (!empty($bottomleft) && file_exists(DIR_IMAGE . $bottomleft)) {
            $image = new Image(DIR_IMAGE . $bottomleft);
            $image->resize($width * ($scale / 100), $height * ($scale / 100));
            $this->imagecopymerge_alpha($original->image, $image->image, 0, imagesy($original->image) - imagesy($image->image), 
                    0, 0, imagesx($image->image), imagesy($image->image), 100);
            imagesavealpha($original->image, true);
            unset($image);
        }
        if (!empty($bottomright) && file_exists(DIR_IMAGE . $bottomright)) {
            $image = new Image(DIR_IMAGE . $bottomright);
            $image->resize($width * ($scale / 100), $height * ($scale / 100));
            $this->imagecopymerge_alpha($original->image, $image->image, imagesx($original->image) - imagesx($image->image), imagesy($original->image) - imagesy($image->image), 0, 0, imagesx($image->image), imagesy($image->image), 100);
            imagesavealpha($original->image, true);
            unset($image);
        }

        if (!empty($productid))
            $newfilename = 'cache/' . dirname($imagefield) . '/' . $productid . '_' . '-' . $width . 'x' . $height . '.' . $extension;
        else
            $newfilename = 'cache/' . dirname($imagefield) . '/' . $filenameOnly . '_' . '-' . $width . 'x' . $height . '.' . $extension;
        $original->save(DIR_IMAGE . $newfilename);
        unset($original);
        return $http . $newfilename;
    }

    function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
        if (!isset($pct)) {
            return false;
        }
        $pct /= 100;
        // Get image width and height
        $w = imagesx($src_im);
        $h = imagesy($src_im);
        // Turn alpha blending off
        imagealphablending($src_im, false);
        // Find the most opaque pixel in the image (the one with the smallest alpha value)
        $minalpha = 127;
        for ($x = 0; $x < $w; $x++)
            for ($y = 0; $y < $h; $y++) {
                $alpha = ( imagecolorat($src_im, $x, $y) >> 24 ) & 0xFF;
                if ($alpha < $minalpha) {
                    $minalpha = $alpha;
                }
            }
        //loop through image pixels and modify alpha for each
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                //get current alpha value (represents the TANSPARENCY!)
                $colorxy = imagecolorat($src_im, $x, $y);
                $alpha = ( $colorxy >> 24 ) & 0xFF;
                //calculate new alpha
                if ($minalpha !== 127) {
                    $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha );
                } else {
                    $alpha += 127 * $pct;
                }
                //get the color index with new alpha
                $alphacolorxy = imagecolorallocatealpha($src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha);
                //set pixel with the new color + opacity
                if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) {
                    return false;
                }
            }
        }

        // The image copy
        imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
    }

}

?>
