Webmaster общности: Predpriemach.com | SearchEngines.bg

    Thumbnail script ver2

    Ето по-гъвкава версия на кода за правене на thumbnail.
    Можете да избирате какъв вид картинка да създава (PNG, JPG, BMP, GIF).
    Също така работи и ако картинката е на отдалечен хост.

    Създавате файл thumb.php и в него пишете следния код:

    <?php
    
    $max_dimension = $_GET['wdth'];
    $source_file = $_GET['from'];
    
    list($img_width,$img_height) = getimagesize($source_file); // Взимат се височината и ширината на картината
    $aspect_ratio = $img_width / $img_height;
    
    if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) )
    {
    if ( $img_width > $img_height ) // За широки картини
    {
    $new_width = $max_dimension;
    $new_height = $new_width / $aspect_ratio;
    }
    elseif ( $img_width < $img_height ) // За високи картини
    {
    $new_height = $max_dimension;
    $new_width = $new_height * $aspect_ratio;
    }
    elseif ( $img_width == $img_height ) // За квадратни картини
    {
    $new_width = $max_dimension;
    $new_height = $max_dimension;
    }
    else { echo "Error reading image size."; return FALSE; }
    }
    else { $new_width = $img_width; $new_height = $img_height; } // Ако е по-малка, размера не се променя
    
    // Make sure these are integers.
    $new_width = intval($new_width);
    $new_height = intval($new_height);
    
    $thumbnail = imagecreatetruecolor($new_width,$new_height); // Създава се картинка в памета
    
    // Взимаме сорса на картинката
    if ( strpos($source_file,".gif") ) { $img_source = imagecreatefromgif($source_file); }
    if ( (strpos($source_file,".jpg")) || (strpos($source_file,".jpeg")) )
    { $img_source = imagecreatefromjpeg($source_file); }
    if ( strpos($source_file,".bmp") ) { $img_source = imagecreatefromwbmp($source_file); }
    if ( strpos($source_file,".png") ) { $img_source = imagecreatefrompng($source_file); }
    
    // Създаваме картинка. В примера е PMG
    header("Content-type: image/jpeg");
    Imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height,imagesx($img_source),imagesy($img_source));
    imagePNG($thumbnail);
    
    // След като сме създали всичко, можем да изчистим картинките.
    imagedestroy($img_source);
    imagedestroy($thumbnail);
    
    ?>

    За да промените вида на картината, която се създава, променете следните редове.
    header(„Content-type: image/png“);
    и
    imagePNG($thumbnail);

    Ето пример как можете да ползвате кода:

    <img src=“thumb.php?from=http://web-tourist.net/new/images/bg1.gif &wdth=1000 “ width=“1000″>

    Където на мястото на http://web-tourist.net/new/images/bg1.gif пишете адреса към картината и на мястото на 1000 пишете новата ширина.

    Това е всичко.
    Урока е писан специално за http://web-tourist.net/
    Ако имате въпроси – питайте във форума!