function generateImage($source_file, $cache_file, $resolution)
{
    global $sharpen, $jpg_quality;
    $extension = strtolower(pathinfo($source_file, PATHINFO_EXTENSION));
    // Check the image dimensions
    $dimensions = GetImageSize($source_file);
    $width = $dimensions[0];
    $height = $dimensions[1];
    // Do we need to downscale the image?
    if ($width <= $resolution) {
        // no, because the width of the source image is already less than the client width
        return $source_file;
    }
    // We need to resize the source image to the width of the resolution breakpoint we're working with
    $ratio = $height / $width;
    $new_width = $resolution;
    $new_height = ceil($new_width * $ratio);
    $dst = ImageCreateTrueColor($new_width, $new_height);
    // re-sized image
    switch ($extension) {
        case 'png':
            $src = @ImageCreateFromPng($source_file);
            // original image
            break;
        case 'gif':
            $src = @ImageCreateFromGif($source_file);
            // original image
            break;
        default:
            $src = @ImageCreateFromJpeg($source_file);
            // original image
            ImageInterlace($dst, true);
            // Enable interlancing (progressive JPG, smaller size file)
            break;
    }
    if ($extension == 'png') {
        imagealphablending($dst, false);
        imagesavealpha($dst, true);
        $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
        imagefilledrectangle($dst, 0, 0, $new_width, $new_height, $transparent);
    }
    ImageCopyResampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // do the resize in memory
    ImageDestroy($src);
    // sharpen the image?
    // NOTE: requires PHP compiled with the bundled version of GD (see http://php.net/manual/en/function.imageconvolution.php)
    if ($sharpen == TRUE && function_exists('imageconvolution')) {
        $intSharpness = findSharp($width, $new_width);
        $arrMatrix = array(array(-1, -2, -1), array(-2, $intSharpness + 12, -2), array(-1, -2, -1));
        imageconvolution($dst, $arrMatrix, $intSharpness, 0);
    }
    $cache_dir = dirname($cache_file);
    // does the directory exist already?
    if (!is_dir($cache_dir)) {
        if (!mkdir($cache_dir, 0755, true)) {
            // check again if it really doesn't exist to protect against race conditions
            if (!is_dir($cache_dir)) {
                // uh-oh, failed to make that directory
                ImageDestroy($dst);
                sendErrorImage("Failed to create cache directory: {$cache_dir}");
            }
        }
    }
    if (!is_writable($cache_dir)) {
        sendErrorImage("The cache directory is not writable: {$cache_dir}");
    }
    // save the new file in the appropriate path, and send a version to the browser
    switch ($extension) {
        case 'png':
            $gotSaved = ImagePng($dst, $cache_file);
            break;
        case 'gif':
            $gotSaved = ImageGif($dst, $cache_file);
            break;
        default:
            $gotSaved = ImageJpeg($dst, $cache_file, $jpg_quality);
            break;
    }
    ImageDestroy($dst);
    if (!$gotSaved && !file_exists($cache_file)) {
        sendErrorImage("Failed to create image: {$cache_file}");
    }
    return $cache_file;
}
示例#2
0
            if (strlen($color) == 3) {
                $background = imagecolorallocate($dst, hexdec($color[0] . $color[0]), hexdec($color[1] . $color[1]), hexdec($color[2] . $color[2]));
            }
        }
        if ($background) {
            imagefill($dst, 0, 0, $background);
        }
    }
}
// Resample the original image into the resized canvas we set up earlier
ImageCopyResampled($dst, $src, 0, 0, $offsetX, $offsetY, $tnWidth, $tnHeight, $width, $height);
if ($doSharpen) {
    // Sharpen the image based on two things:
    //	(1) the difference between the original size and the final size
    //	(2) the final size
    $sharpness = findSharp($width, $tnWidth);
    $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1));
    $divisor = $sharpness;
    $offset = 0;
    imageconvolution($dst, $sharpenMatrix, $divisor, $offset);
}
// Make sure the cache exists. If it doesn't, then create it
if (!file_exists(CACHE_DIR)) {
    mkdir(CACHE_DIR, 0755);
}
// Make sure we can read and write the cache directory
if (!is_readable(CACHE_DIR)) {
    header('HTTP/1.1 500 Internal Server Error');
    echo 'Error: the cache directory is not readable';
    exit;
} else {
示例#3
0
    if ($imageMimetype === 'image/jpeg') {
        $output_function = 'imagepng';
        $imageMimetype = 'image/png';
        $do_sharpen = false;
        $quality = round(10 - $quality / 10);
    }
}
// Imagerotate
if (ENABLE_IMAGEROTATE && !empty($angle)) {
    $destination_image = imagerotate($destination_image, $angle, $background, 0);
}
if ($do_sharpen) {
    // Sharpen the image based on two things:
    // (1) the difference between the original size and the final size
    // (2) the final size
    $sharpness = findSharp($imageWidth, $tn_width);
    $sharpen_matrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1));
    $divisor = $sharpness;
    $offset = 0;
    imageconvolution($destination_image, $sharpen_matrix, $divisor, $offset);
}
// Put the data of the resized image into a variable
ob_start();
$output_function($destination_image, null, $quality);
$imageData = ob_get_contents();
ob_end_clean();
// Update $image_created_time
$imageCreatedTime = time();
// Clean up the memory
imagedestroy($sourceImage);
imagedestroy($destination_image);
示例#4
0
 /**
  * @param float $factor
  * @return ImageEditorGD $instance
  */
 public function sharpen($factor = 1)
 {
     if (!function_exists('findSharp')) {
         function findSharp($orig, $final)
         {
             $final = $final * (750.0 / $orig);
             $a = 52;
             $b = -0.27810650887573124;
             $c = 0.00047337278106508946;
             $result = $a + $b * $final + $c * $final * $final;
             return max(round($result), 0);
         }
     }
     $sharpness = findSharp($this->currentWidth, $this->currentWidth * $factor);
     $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1));
     $divisor = $sharpness;
     $offset = 0;
     imageconvolution($this->sourceImage, $sharpenMatrix, $divisor, $offset);
     return $this;
 }
示例#5
0
function scaleImage($pic, $dir_origin, $dir_target, $maxWidth, $maxHeight, $make_thumbs = false)
{
    # nichts machen $maxWidth und $maxHeight sind leer
    if (empty($maxHeight) and empty($maxWidth)) {
        return;
    }
    if (!extension_loaded("gd")) {
        return;
    }
    // --------------------------------------------------------------------
    // Bildgröße und MIME Type holen
    // --------------------------------------------------------------------
    $size = @GetImageSize($dir_origin . $pic);
    $mime = $size['mime'];
    $width = $size[0];
    $height = $size[1];
    // --------------------------------------------------------------------
    // Variablen
    // --------------------------------------------------------------------
    if (empty($maxHeight) and !empty($maxWidth)) {
        $maxHeight = $height / $width * $maxWidth;
    }
    if (empty($maxWidth) and !empty($maxHeight)) {
        $maxWidth = $width / $height * $maxHeight;
    }
    // --------------------------------------------------------------------
    // Sicherheitsüberprüfungen
    // --------------------------------------------------------------------
    // Der Bildname darf folgende Zeichen nicht enthalten / : .. < >
    if (strpos($pic, ':') || preg_match('/(\\.\\.|<|>)/', $pic)) {
        die("Error: Bilddatei " . $dir_origin . $pic . "enthält nicht gültige Zeichen!");
    }
    // Handelt es sich bei der Datei auch wirklich um ein Bild
    if (substr($mime, 0, 6) != 'image/') {
        return 0;
    }
    // --------------------------------------------------------------------
    // Die Seitenverhältnisse von Breite zu Höhe und Höhe zu Breite ermitteln,
    // und dann die Breite und Höhe für das Vorschaubild ermitteln,
    // aber nur, wenn das Originalbild größer als das Zielbild ist
    // --------------------------------------------------------------------
    $xRatio = $maxWidth / $width;
    $yRatio = $maxHeight / $height;
    if ($xRatio * $height < $maxHeight) {
        // Bildmaße auf Basis der Breite
        $tnHeight = ceil($xRatio * $height);
        $tnWidth = $maxWidth;
    } else {
        $tnWidth = ceil($yRatio * $width);
        $tnHeight = $maxHeight;
    }
    # Bild grösse <= Neue grösse also nicht zu tun
    if ($width <= $tnWidth and $height <= $tnHeight) {
        # Vorschaubilder neu erzwingen $make_thumbs = true
        if ($make_thumbs === false) {
            return;
        }
    }
    // --------------------------------------------------------------------
    // Hauptteil zum Scalieren erstellen
    // --------------------------------------------------------------------
    // Welche Funktionen sollen genutzt werden um die Vorschaubilder zu erzeugen
    switch ($size['mime']) {
        // Damit werden GIFs verarbeitet
        case 'image/gif':
            $creationFunction = 'ImageCreateFromGif';
            $outputFunction = 'ImageGif';
            $doSharpen = TRUE;
            break;
            // Damit werden PNGs verarbeitet
        // Damit werden PNGs verarbeitet
        case 'image/x-png':
        case 'image/png':
            $creationFunction = 'ImageCreateFromPng';
            $outputFunction = 'ImagePng';
            $doSharpen = TRUE;
            // PNG braucht einen Kompressionslevel 0 (Keine Kompression) bis 9 - (5 sollte ausreichen für Vorschaubilder)
            $quality = 5;
            break;
            // Damit werden JPEGs verarbeitet
        // Damit werden JPEGs verarbeitet
        case 'image/pipeg':
        case 'image/jpeg':
        case 'image/pjpeg':
            $creationFunction = 'ImageCreateFromJpeg';
            $outputFunction = 'ImageJpeg';
            $doSharpen = TRUE;
            $quality = 65;
            break;
            // alles andere wird einfach copiert (Sollte noch verbessert werden!)
        // alles andere wird einfach copiert (Sollte noch verbessert werden!)
        default:
            return;
            break;
    }
    // Das Quellbild in ein Objekt laden
    $src = $creationFunction($dir_origin . $pic);
    // Ein leeres Objekt für das Ziel anlegen
    $dst = imagecreatetruecolor($tnWidth, $tnHeight);
    // Transparenz im Bild einschalten (nur GIF und PNG)
    if (in_array($size['mime'], array('image/gif', 'image/png'))) {
        $transparencyIndex = imagecolortransparent($src);
        $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
        if ($transparencyIndex >= 0) {
            $transparencyColor = @imagecolorsforindex($src, $transparencyIndex);
            $transparencyIndex = imagecolorallocate($dst, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
            imagefill($dst, 0, 0, $transparencyIndex);
            imagecolortransparent($dst, $transparencyIndex);
            // Wenn GIF und Transparenz gefunden, dann DO_SHARPEN ausschalten.
            // Führt sonst zu unschönen ergebnissen
            if (in_array($size['mime'], array('image/gif'))) {
                $doSharpen = FALSE;
            }
        }
    }
    // Jetzt wird das Bild in das Objekt $dst geladen und die grösse verändert
    ImageCopyResampled($dst, $src, 0, 0, 0, 0, $tnWidth, $tnHeight, $width, $height);
    // Hier wird versucht das Zielbild noch etwas schärfer zu bekommen
    // Das Basiert auf zwei dingen
    // 1. Die Different der Quell- und Zielgrösse
    // 2. Der Finalen Grösse
    if ($doSharpen) {
        $sharpness = findSharp($width, $tnWidth);
        $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1));
        $divisor = $sharpness;
        $offset = 0;
        imageconvolution($dst, $sharpenMatrix, $divisor, $offset);
    }
    // Das Zielbild speichern
    $outputFunction($dst, $dir_target . $pic, $quality);
    changeChmod($dir_target . $pic);
    // Aufräumen
    ImageDestroy($src);
    ImageDestroy($dst);
}
示例#6
0
    $src = @ImageCreateFromPng($source_image); // original image
  break;
  case 'gif':
    $src = @ImageCreateFromGif($source_image); // original image
  break;
  default:
    $src = @ImageCreateFromJpeg($source_image); // original image
  break;
}

$dst = ImageCreateTrueColor($new_width,$new_height); // re-sized image
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // do the resize in memory

// sharpen the image?
if($sharpen == TRUE) {
  $intSharpness = findSharp($width, $new_width);
  $arrMatrix = array(
      array(-1, -2, -1),
      array(-2, $intSharpness + 12, -2),
      array(-1, -2, -1)
  );
  imageconvolution($dst, $arrMatrix, $intSharpness, 0);
}

// check the path directory exists and is writable
$directories = str_replace("/$requested_file","",$requested_uri); // get the directories only
$directories = substr($directories,1); // clean the string

if(!is_dir("$document_root/$cache_path/$resolution/$directories")){ // does the directory exist already?
  if (!mkdir("$document_root/$cache_path/$resolution/$directories", 0777, true)) { // make the directory
    // uh-oh, failed to make that directory