Создание превьюшек

Spectrum

Guest
Создание превьюшек

Целесообразно ли использовать imagecopyresampled если его одновременно будут пользовать человек 100 с файлами максимум 200кб ?
Как подсчитать расход памяти ? ну как минимум 20Мб сами изображения в этом случае + ...
И на много ли лучше использовать Image Magic ? (не разу его не пробовал - пока даже не знаю как к нему подступиться и стоит ли ;) )

И подскажите можно ли в пхп - сделать шарп и блюр на изображении ? Если да то чем .

Спасибо.
 

SiMM

Новичок
Целесообразно превьюшки кэшировать (или делать их раз и навсегда). Тогда вопрос об одновременном использовании imagecopyresampled при обработке 100 запросов одновременно отпадёт сам собой.
 

kruglov

Новичок
Автор оригинала: Spectrum
да , именно так делаются один раз
В чем тогда вопрос?

Sharp и Blur GD не умеет. Вручную шарпить - слишком долго. Так что приступайте к imagemagickу
 

Spectrum

Guest
Автор оригинала: kruglov
В чем тогда вопрос?
вопрос в том сколько понадобится памяти для одновременной работы ...

по шарпу и блюру понял - подскажите пожалуйста где можно посмотреть исходники с примерами для IM ?
 

magic

lancer
Пофиксенный плагин для Smarty для создания превьюшек.
PHP:
function smarty_function_thumb($params, &$smarty) {
    extract($params);

    // Start time measurement
    if (isset($dev) && $dev) {
        if (!function_exists('getmicrotime')) {
            function getmicrotime() {
                list($usec, $sec) = explode(" ",microtime());
                return ((float)$usec + (float)$sec);
            }
        }
        $time['start'] = getmicrotime();
    }

    // Funktion zum Sch�fen
    if (!function_exists('UnsharpMask')) {
        // Unsharp mask algorithm by Torstein Hnsi 2003 (thoensi_at_netcom_dot_no)
        // Christoph Erdmann: changed it a little, cause i could not reproduce the darker blurred image, now it is up to 15% faster with same results
        function UnsharpMask($img, $amount, $radius, $threshold)
            {
            // Attempt to calibrate the parameters to Photoshop:
            if ($amount > 500) $amount = 500;
            $amount = $amount * 0.016;
            if ($radius > 50) $radius = 50;
            $radius = $radius * 2;
            if ($threshold > 255) $threshold = 255;

            $radius = abs(round($radius));  // Only integers make sense.
            if ($radius == 0) {     return $img; imagedestroy($img); break; }
            $w = imagesx($img); $h = imagesy($img);
            $imgCanvas = $img;
            $imgCanvas2 = $img;
            $imgBlur = imagecreatetruecolor($w, $h);

            // Gaussian blur matrix:
            //      1       2       1
            //      2       4       2
            //      1       2       1

            // Move copies of the image around one pixel at the time and merge them with weight
            // according to the matrix. The same matrix is simply repeated for higher radii.
            for ($i = 0; $i < $radius; $i++) {
                imagecopy      ($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // up left
                imagecopymerge ($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right
                imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
                imagecopymerge ($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right
                imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
                imagecopymerge ($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right
                imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20 ); // up
                imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down
                imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center
            }
            $imgCanvas = $imgBlur;

            // Calculate the difference between the blurred pixels and the original
            // and set the pixels
            for ($x = 0; $x < $w; $x++) {
                // each row
                for ($y = 0; $y < $h; $y++) {
                    // each pixel
                    $rgbOrig = ImageColorAt($imgCanvas2, $x, $y);
                    $rOrig = (($rgbOrig >> 16) & 0xFF);
                    $gOrig = (($rgbOrig >> 8) & 0xFF);
                    $bOrig = ($rgbOrig & 0xFF);
                    $rgbBlur = ImageColorAt($imgCanvas, $x, $y);
                    $rBlur = (($rgbBlur >> 16) & 0xFF);
                    $gBlur = (($rgbBlur >> 8) & 0xFF);
                    $bBlur = ($rgbBlur & 0xFF);

                    // When the masked pixels differ less from the original
                    // than the threshold specifies, they are set to their original value.
                    $rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
                    $gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
                    $bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;

                    if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
                        $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
                        ImageSetPixel($img, $x, $y, $pixCol);
                    }
                }
            }
            return $img;
        }
    }

    $_CONFIG['types'] = array('','.gif','.jpg','.png');


    ### &#65533;ergebene Parameter auswerten und verifizieren
    if (empty($cache)) $_CONFIG['cache'] = 'images/cache/';
    else $_CONFIG['cache'] = $cache;

    if (empty($file) || !file_exists($file)) { $smarty->_trigger_fatal_error("thumb: parameter 'file' cannot be empty and must exist"); return; }
    if (!isset($link) || empty($link)) $link = true;
    if (!isset($window) || empty($window)) $window = true;
    if (!isset($hint) || empty($hint)) $hint = true;
    if (!isset($extrapolate) || empty($extrapolate)) $extrapolate = true;
    if (!isset($dev) || empty($dev)) $crop = false;
    if (!isset($crop) || empty($crop)) $crop = true;
    if (empty($width) && empty($height) AND empty($longside) AND empty($shortside)) $width = 100;

    ### Info ber Source (SRC) holen
    $temp = getimagesize($file);

    $_SRC['file']     = $file;
    $_SRC['width']    = $temp[0];
    $_SRC['height']   = $temp[1];
    $_SRC['type']     = $temp[2]; // 1=GIF, 2=JPG, 3=PNG, SWF=4
    $_SRC['string']   = $temp[3];
    $_SRC['filename'] = basename($file);
    $_SRC['modified'] = filemtime($file);

    // Hash erstellen
    $_SRC['hash']     = md5($_SRC['file'].$_SRC['modified'].implode('',$params));


    ### Infos ber Destination (DST) errechnen
    if (isset($width) && is_numeric($width)) $_DST['width'] = $width;
    else $_DST['width'] = round($height/($_SRC['height']/$_SRC['width']));

    if (isset($height) && is_numeric($height)) $_DST['height'] = $height;
    else $_DST['height'] = round($width/($_SRC['width']/$_SRC['height']));

    // Das Gr&#65533;enverh&#65533;tnis soll erhalten bleiben egal ob das Bild hoch oder querformatig ist.
    if (isset($longside) && is_numeric($longside)) {
        if ($_SRC['width'] < $_SRC['height']) {
            $_DST['height'] = $longside;
            $_DST['width']  = round($longside/($_SRC['height']/$_SRC['width']));
        } else {
            $_DST['width']  = $longside;
            $_DST['height'] = round($longside/($_SRC['width']/$_SRC['height']));
        }
    } elseif (isset($shortside) && is_numeric($shortside)) {
        if ($_SRC['width'] < $_SRC['height']) {
            $_DST['width']  = $shortside;
            $_DST['height'] = round($shortside/($_SRC['width']/$_SRC['height']));
        } else {
            $_DST['height'] = $shortside;
            $_DST['width']  = round($shortside/($_SRC['height']/$_SRC['width']));
        }
    }

    // Soll beschnitten werden? (Standard)
    if(isset($crop) && $crop) {
        $width_ratio = $_SRC['width']/$_DST['width'];
        $height_ratio = $_SRC['height']/$_DST['height'];

        // Es muss an der Breite beschnitten werden
        if ($width_ratio > $height_ratio) {
            $_DST['offset_w'] = round(($_SRC['width']-$_DST['width']*$height_ratio)/2);
            $_DST['offset_h'] = 0;
            $_SRC['width'] = round($_DST['width']*$height_ratio);
        // es muss an der H&#65533;e beschnitten werden
        } elseif ($width_ratio < $height_ratio) {
            $_DST['offset_h'] = round(($_SRC['height']-$_DST['height']*$width_ratio)/2);
            $_DST['offset_w'] = 0;
            $_SRC['height'] = round($_DST['height']*$width_ratio);
        }
    } else {
        $_DST['offset_w'] = 0;
        $_DST['offset_h'] = 0;
    }

    // Wenn das Ursprungsbild kleiner als das Ziel-Bild ist, soll nicht hochskaliert werden und die neu berechneten Werte werden wieder berschrieben
    if (isset($extrapolate) && ($extrapolate == 'false') && $_DST['height'] > $_SRC['height'] && $_DST['width'] > $_SRC['width']) {
        $_DST['width'] = $_SRC['width'];
        $_DST['height'] = $_SRC['height'];
    }

    if (!empty($params['type'])) $_DST['type']      = $params['type'];
    else $_DST['type']      = $_SRC['type'];

    $_DST['file']           = $_CONFIG['cache'].$_SRC['hash'].$_CONFIG['types'][$_DST['type']];
    $_DST['string']         = 'width="'.$_DST['width'].'" height="'.$_DST['height'].'"';


    ### Rckgabe-Strings erstellen
    if (isset($html) && !empty($html)) $_RETURN['img'] = '<img src="'.$_DST['file'].'" '.$html.' '.$_DST['string'].' />';
    else $_RETURN['img'] = '<img src="'.$_DST['file'].'" '.$_DST['string'].' alt="" title="" />';

    if ($link == "true") {
        if (empty($params['linkurl'])) $params['linkurl'] = $_SRC['file'];

        if ($window == "true") $returner = '<a href="'.$params['linkurl'].'" target="_blank">'.$_RETURN['img'].'</a>';
        else $returner = '<a href="'.$params['linkurl'].'">'.$_RETURN['img'].'</a>';
    } else {
        $returner = $_RETURN['img'];
    }

    ### Cache-Datei abfangen
    if (file_exists($_DST['file']) && (!isset($dev) || !$dev)) return $returner;

    ### ansonsten weitermachen

    // SRC einlesen
    if ($_SRC['type'] == 1) $_SRC['image'] = imagecreatefromgif($_SRC['file']);
    if ($_SRC['type'] == 2) $_SRC['image'] = imagecreatefromjpeg($_SRC['file']);
    if ($_SRC['type'] == 3) $_SRC['image'] = imagecreatefrompng($_SRC['file']);

    // Wenn das Bild sehr gro&#65533;ist, zuerst linear auf vierfache Zielgr&#65533;e herunterskalieren und $_SRC berschreiben
    if ($_DST['width']*4 < $_SRC['width'] && $_DST['height']*4 < $_SRC['height']) {
        // Multiplikator der Zielgr&#65533;e
        $_TMP['width'] = round($_DST['width']*4);
        $_TMP['height'] = round($_DST['height']*4);

        $_TMP['image'] = imagecreatetruecolor($_TMP['width'], $_TMP['height']);
        imagecopyresized($_TMP['image'], $_SRC['image'], 0, 0, $_DST['offset_w'], $_DST['offset_h'], $_TMP['width'], $_TMP['height'], $_SRC['width'], $_SRC['height']);
        $_SRC['image'] = $_TMP['image'];
        $_SRC['width'] = $_TMP['width'];
        $_SRC['height'] = $_TMP['height'];

        // Wenn vorskaliert wird, darf ja nicht nochmal ein bestimmter Bereich ausgeschnitten werden
        $_DST['offset_w'] = 0;
        $_DST['offset_h'] = 0;
        unset($_TMP['image']);
    }

    // DST erstellen
    $_DST['image'] = imagecreatetruecolor($_DST['width'], $_DST['height']);
    imagecopyresampled($_DST['image'], $_SRC['image'], 0, 0, $_DST['offset_w'], $_DST['offset_h'], $_DST['width'], $_DST['height'], $_SRC['width'], $_SRC['height']);
    if (isset($sharpen) && $sharpen != "false") $_DST['image'] = UnsharpMask($_DST['image'],80,.5,3);

    // Soll eine Lupe eingefgt werden?
    if ($hint == "true" && $link == "true") {
        //Soll der wei&#65533; Balken wirklich hinzugefgt werden?
        if (!isset($addgreytohint) || ($addgreytohint != 'false')) {
            $trans = imagecolorallocatealpha($_DST['image'], 255, 255, 255, 25);
            imagefilledrectangle($_DST['image'], 0, $_DST['height']-9, $_DST['width'], $_DST['height'], $trans);
        }

        $magnifier = imagecreatefromstring(gzuncompress(base64_decode("eJzrDPBz5+WS4mJgYOD19HAJAtLcIMzBBiRXrilXA1IsxU6eIRxAUMOR0gHkcxZ4RBYD1QiBMOOlu3V/gIISJa4RJc5FqYklmfl5CiGZuakMBoZ6hkZ6RgYGJs77ex2BalRBaoLz00rKE4tSGXwTk4vyc1NTMhMV3DKLUsvzi7KLFXwjFEAa2svWnGdgYPTydHEMqZhTOsE++1CAyNHzm2NZjgau+dAmXlAwoatQmOld3t/NPxlLMvY7sovPzXHf7re05BPzjpQTMkZTPjm1HlHkv6clYWK43Zt16rcDjdZ/3j2cd7qD4/HHH3GaprFrw0QZDHicORXl2JsPsveVTDz//L3N+WpxJ5Hff+10Tjdd2/Vi17vea79Om5w9zzyne9GLnWGrN8atby/ayXPOsu2w4quvVtxNCVVz5nAf3nDpZckBCedpqSc28WTOWnT7rZNXZSlPvFybie9EFc6y3bIMCn3JAoJ+kyyfn9qWq+LZ9Las26Jv482cDRE6Ci0B6gVbo2oj9KabzD8vyMK4ZMqMs2kSvW4chz88SXNzmeGjtj1QZK9M3HHL8L7HITX3t19//VVY8CYDg9Kvy2vDXu+6mGGxNOiltMPsjn/t9eJr0ja/FOdi5TyQ9Lz3fOqstOr99/dnro2vZ1jy76D/vYivPsBoYPB09XNZ55TQBAAJjs5s</body>")));
        imagealphablending($_DST['image'], true);
        imagecopy($_DST['image'], $magnifier, $_DST['width']-15, $_DST['height']-14, 0, 0, 11, 11);
        imagedestroy($magnifier);
    }

    // Berechnungszeit hinzufgen
    if (isset($dev) && $dev) {
        // Zeit anhalten
        $time['end'] = getmicrotime();
        $time = round($time['end'] - $time['start'],2);

        // Farben definieren
        $white_trans = imagecolorallocatealpha($_DST['image'], 255, 255, 255, 25);
        $black = ImageColorAllocate ($_DST['image'], 0, 0, 0);

        // Wei&#65533;r Balken oben
        imagefilledrectangle($_DST['image'], 0, 0, $_DST['width'], 10, $white_trans);

        // Schrift mit Zeitangabe
        imagestring($_DST['image'], 1, 5, 2, 'processing time: '.$time.'s', $black);
    }

    // Thumbnail abspeichern
    if ($_DST['type'] == 1) {
        imagetruecolortopalette($_DST['image'], false, 256);
        imagegif($_DST['image'], $_DST['file']);
    }

    if ($_DST['type'] == 2) {
        if (empty($params['quality'])) $params['quality'] = 80;
        imagejpeg($_DST['image'], $_DST['file'],$params['quality']);
    }

    if ($_DST['type'] == 3) {
        imagepng($_DST['image'], $_DST['file']);
    }

    imagedestroy($_DST['image']);
    imagedestroy($_SRC['image']);

    // Und Bild ausgeben
    return $returner;

}
-~{}~ 22.12.05 20:58:

Не совсем IM, но работает и кеширует.
 

kruglov

Новичок
вопрос в том сколько понадобится памяти для одновременной работы
А почему одновременной? В смысле вот выложили вы готовый сайт без превьюшек и тут же на него накинулось 1000 посетителей превьюшки генерить?

Пройдитесь сами по сайту каким-нить download managerом, и пущай по его неодновременным запросам сгенерятся превьюшки.
 

SashkoSoft

Новичок
приступайте к imagemagickу

Автор оригинала: kruglov
приступайте к imagemagickу
А вы случайно не подскажите, как мне решить такую проблемку:

стоит у меня под вин. PHP Version 4.3.7.
скачал у них длл-ки:
php-4.4.0_magickwand_dyn.dll и т.д.
в php.ini прописал:

extension=php_magickwand_dyn.dll
extension=php_magickwand_q32_st.dll
extension=php_magickwand_q16_st.dll
extension=php_magickwand_q8_st.dll

и положил их в C:\PHP\extensions\
Appach рестартнул...
а при запуске скрипта получаю ошибку что Unable to load dyn lib c:\php\extensions\php_magickwand_dyn.dll
и т.д.
The specified module could not be found.
Вот вопрос должна-ли версия пхп четко соответствовать версии magickwand иди причина не в этом?
 
Сверху