示例#1
0
function createThumb($file, $ext, $width)
{
    $im = '';
    $im = openImage($file);
    if (empty($im)) {
        return false;
    }
    $old_x = imagesx($im);
    $old_y = imagesy($im);
    $new_w = (int) $width;
    if ($new_w <= 0 or $new_w > $old_x) {
        $new_w = $old_x;
    }
    $new_h = $old_x * ($new_w / $old_x);
    if ($old_x > $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $old_y * ($new_h / $old_x);
    }
    if ($old_x < $old_y) {
        $thumb_w = $old_x * ($new_w / $old_y);
        $thumb_h = $new_h;
    }
    if ($old_x == $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $new_h;
    }
    $thumb = imagecreatetruecolor($thumb_w, $thumb_h);
    if ($ext == 'png') {
        imagealphablending($thumb, false);
        $colorTransparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
        imagefill($thumb, 0, 0, $colorTransparent);
        imagesavealpha($thumb, true);
    } elseif ($ext == 'gif') {
        $trnprt_indx = imagecolortransparent($im);
        if ($trnprt_indx >= 0) {
            //its transparent
            $trnprt_color = imagecolorsforindex($im, $trnprt_indx);
            $trnprt_indx = imagecolorallocate($thumb, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
            imagefill($thumb, 0, 0, $trnprt_indx);
            imagecolortransparent($thumb, $trnprt_indx);
        }
    }
    imagecopyresampled($thumb, $im, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
    //choose which image program to use
    switch (strtolower($ext)) {
        case 'jpg':
        case 'jpeg':
            imagejpeg($thumb, $file);
            break;
        case 'gif':
            imagegif($thumb, $file);
            break;
        case 'png':
            imagepng($thumb, $file);
            break;
        default:
            return false;
            break;
    }
    imagedestroy($im);
    imagedestroy($thumb);
}
示例#2
0
	function fingerprint($src_or_resource) {
		$thumbWidth = 150;
		$sensitivity = 2;

		if (!$image = @openImage($src_or_resource)) return -1;


	// Create thumbnail sized copy for fingerprinting
		$width = imagesx($image);
		$height = imagesy($image);
		$ratio = $thumbWidth / $width;
		$newwidth = $thumbWidth;
		$newheight = round($height * $ratio); 
		$smallimage = imagecreatetruecolor($newwidth, $newheight);
		imagecopyresampled($smallimage, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		$palette = imagecreatetruecolor(1, 1);
		$gsimage = imagecreatetruecolor($newwidth, $newheight);

	// Convert each pixel to greyscale, round it off, and add it to the histogram count
		$numpixels = $newwidth * $newheight;
		$histogram = array();
		for ($i = 0; $i < $newwidth; $i++) {
			for ($j = 0; $j < $newheight; $j++) {
				$pos = imagecolorat($smallimage, $i, $j);
				$cols = imagecolorsforindex($smallimage, $pos);
				$r = $cols['red'];
				$g = $cols['green'];
				$b = $cols['blue'];
				// Convert the colour to greyscale using 30% Red, 59% Blue and 11% Green
				$greyscale = round(($r * 0.3) + ($g * 0.59) + ($b * 0.11));                 
				$greyscale++;
				$value = (round($greyscale / 16) * 16) -1;
				@$histogram[$value]++;
			}
		}

	// Normalize the histogram by dividing the total of each colour by the total number of pixels
		$normhist = array();
		foreach ($histogram as $value => $count) {
			$normhist[$value] = $count / $numpixels;
		}

	// Find maximum value (most frequent colour)
		$max = 0;
		for ($i=0; $i<255; $i++) {
			if (@$normhist[$i] > $max) {
				$max = $normhist[$i];
			}
		}   

	// Create a string from the histogram (with all possible values)
		$histstring = "";
		for ($i = -1; $i <= 255; $i = $i + 16) {
			$h = @(@$normhist[$i] / $max) * $sensitivity;
			if ($i < 0) {
				$index = 0;
			} else {
				$index = $i;
			}
			$height = round($h);
			$histstring .= $height;
		}

	// Destroy all the images that we've created
		imagedestroy($image);
		imagedestroy($smallimage);
		imagedestroy($palette);
		imagedestroy($gsimage);

	// Generate an md5sum of the histogram values and return it
		$checksum = md5($histstring);
		return $checksum;

	}