Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Pametan thumbnail - kako?

[es] :: PHP :: Pametan thumbnail - kako?

[ Pregleda: 2183 | Odgovora: 8 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Šahbaz
Developer, BBM
EU

Član broj: 102142
Poruke: 106
95.155.1.*



Profil

icon Pametan thumbnail - kako?17.07.2009. u 16:14 - pre 179 meseci
Nisam siguran da je naslov bas adekvatan ali ono sto mi treba ne znam kako bih drugacije nazvao.

Zamislimo sledeci scenario:

Slika 1 ima dimenzije: 250x375
Slika 2 ima dimenzije: 400x400
Slika 3 ima dimzenije: 300x700

i tako dalje...

Thumbnail treba da izgleda "normalno" i da ima dimenzije 130x100

Medjutim slike ce biti "sabijene" i nece izgledati prirodno.
Resenje bi bio neki pametan (automatski) krop (sto mi djeluje prilicno nemoguce), ili da se slika skalira tako da je iskoriscenje vece a da se ostatak do popunjenja 130x100 popuni crnom bojom ili tako nesto.

Zanima me kako bi trebao da izgleda taj kod, i da li imate neke predloge za resenje ovog problema!?
 
Odgovor na temu

amitkic
student
zvechan

Član broj: 186610
Poruke: 42
82.114.95.*



Profil

icon Re: Pametan thumbnail - kako?17.07.2009. u 17:37 - pre 179 meseci
neshto ovako mozda

Code:

$upload_path='../images/';
$add=$upload_path.$_FILES['userfile']['name'];
                $mw = 130; // max width
        $mh = 100; // max height
        $im=ImageCreateFromJPEG($add);
        $width=ImageSx($im);
        $height=ImageSy($im);
        if( $width > $mw || $height > $mh ) {
            if( $width > $height ) {
            $tnw = $mw;
            $tnh = $tnw * $height / $width;
        } else {
            $tnh = $mh;
            $tnw = $tnh * $width / $height;
            }
        } else {
        $tnw = $ow;
        $tnh = $oh;
            }
        $newimage=imagecreatetruecolor($mw,$mh);
        ImageCopyResized($newimage,$im,0,0,0,0,$thw,$tnh,$width,$height);
        ImageGIF($newimage,$tsrc);



[Ovu poruku je menjao amitkic dana 17.07.2009. u 19:06 GMT+1]
 
Odgovor na temu

Milos911
Serbia

Član broj: 219127
Poruke: 1230
*.telenor.co.yu.



+303 Profil

icon Re: Pametan thumbnail - kako?17.07.2009. u 17:37 - pre 179 meseci
Posto sam se malo pogubio recicemo da je visina thumba 130 a sirina 100
Code:
 
if ($slikasirina>$slikavisina)
   {
   echo "smanji sirinu na 100";
   echo "izracunaj koliko si je puta smanjio pa sacuvaj taj broj u varijablu $procenat";
   echo "ovde smanji visinu za onoliko procenata kolika je vrednost $procenat";
    }
elseif ($slikasirina<$slikavisina)
    {
    echo "smanji visinu na 130";
    echo "ovde smanji sirinu za onoliko procenata kolika je vrednost $procenat";
    }
elseif($slikasirina=$slikavisina)
    {
    echo "smanji visinu i sirinu na zeljene vrednosti";
     }



Evo nadam se da sam ti pomogao :) Samo ne znam kako da ostatak ispunis crnom. Najlakse bi ti bilo da definises u html neke okvire(okvirove?) i ofarbas ih u crno.... Samo stavi neke novine na pod da ga ne uprljas :D
 
Odgovor na temu

Šahbaz
Developer, BBM
EU

Član broj: 102142
Poruke: 106
95.155.1.*



Profil

icon Re: Pametan thumbnail - kako?17.07.2009. u 18:29 - pre 179 meseci
Pozdrav,

Nije mi algoritam problem vec php i nema koriscenja html-a...

Primjer koji je ostavio amitkic ima neke grescice u kodu ali kada se isprave je vrlo blizu resenja - medjutim ovakav kod vec imam.
Potrebno je da slika bude centrirana a da se crno rasporedjuje "oko nje".

Sa ovim kodom to nije slucaj vec GD napravi sliku recimo 100x100 i ostale pixele ispuni crnom recimo na dolje, dok je meni potrebno da crno bude oko kreirane slike (thumbnaila) (sa svih strana) a ne samo sa jedne njene strane jer se na taj nacin gubi smisao...
 
Odgovor na temu

vatri
Banja Luka, RS

Član broj: 68697
Poruke: 1006
62.68.102.*



+18 Profil

icon Re: Pametan thumbnail - kako?17.07.2009. u 18:34 - pre 179 meseci
Ja ti imam neku klasu sa phpclasses.org mozda pomogne, nisam ju testirao (pogledaj attachment)
Prikačeni fajlovi
 
Odgovor na temu

vatri
Banja Luka, RS

Član broj: 68697
Poruke: 1006
62.68.102.*



+18 Profil

icon Re: Pametan thumbnail - kako?17.07.2009. u 18:37 - pre 179 meseci
evo jos jedna, ova ima razmjerno "kropiranje"
Prikačeni fajlovi
 
Odgovor na temu

_CaRtMan_

Član broj: 126731
Poruke: 305
62.68.110.*



+1 Profil

icon Re: Pametan thumbnail - kako?17.07.2009. u 19:09 - pre 179 meseci
Ja koristim ovaj fajl, samo što ćeš morati promijeniti cache directory. Pošto tu sprema thumb-ove pa onda ih čita. Pristupaš ovako:

thumb.php?src=images/fajl.jpg&h=155&w=200&zc=1

Pokušaj učitati gdje je zc = 0 , pa onda sa zc = 1 da vidiš razliku.

Code:

<?php

// TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks
// http://code.google.com/p/timthumb/

// MIT License: http://www.opensource.org/licenses/mit-license.php

/* Parameters allowed: */

// w: width
// h: height
// zc: zoom crop (0 or 1)
// q: quality (default is 75 and max is 100)

// HTML example: <img src="/scripts/timthumb.php?src=/images/whatever.jpg&w=150&h=200&zc=1" alt="" />

error_reporting(E_ALL);

if(!isset($_REQUEST["src"])) {
    die("no image specified");
}

// clean params before use
$src = clean_source( $_REQUEST[ "src" ] );

// set document root
$doc_root = get_document_root($src);

// get path to image on file system
$src = $doc_root . '' . $src;

$new_width = preg_replace( "/[^0-9]+/", "", get_request( 'w', 100 ) );
$new_height = preg_replace( "/[^0-9]+/", "", get_request( 'h', 100 ) );
$zoom_crop = preg_replace( "/[^0-9]+/", "", get_request( 'zc', 1 ) );
$quality = preg_replace( "/[^0-9]+/", "", get_request( '9', 80 ) );

// set path to cache directory (default is ./cache)
// this can be changed to a different location
$cache_dir = 'images/small';

// get mime type of src
$mime_type = mime_type($src);

// check to see if this image is in the cache already
//check_cache($cache_dir, $mime_type);

// make sure that the src is gif/jpg/png
if(!valid_src_mime_type($mime_type)) {
    die("Invalid src mime type: $mime_type");
}

// check to see if GD function exist
if(!function_exists('imagecreatetruecolor')) {
    die("GD Library Error: imagecreatetruecolor does not exist");
}

if(strlen($src) && file_exists($src)) {

    // open the existing image
    $image = open_image($mime_type, $src);
    if($image === false) {
        die('Unable to open image : ' . $src);
    }

    // Get original width and height
    $width = imagesx($image);
    $height = imagesy($image);

    // don't allow new width or height to be greater than the original
    if( $new_width > $width ) {
        $new_width = $width;
    }
    if( $new_height > $height ) {
        $new_height = $height;
    }

    // generate new w/h if not provided
    if( $new_width && !$new_height ) {
    
        $new_height = $height * ( $new_width / $width );
        
    } elseif($new_height && !$new_width) {
    
        $new_width = $width * ( $new_height / $height );
        
    } elseif(!$new_width && !$new_height) {
    
        $new_width = $width;
        $new_height = $height;
        
    }
    
    // create a new true color image
    $canvas = imagecreatetruecolor( $new_width, $new_height );

    if( $zoom_crop ) {

        $src_x = $src_y = 0;
        $src_w = $width;
        $src_h = $height;

        $cmp_x = $width  / $new_width;
        $cmp_y = $height / $new_height;

        // calculate x or y coordinate and width or height of source

        if ( $cmp_x > $cmp_y ) {

            $src_w = round( ( $width / $cmp_x * $cmp_y ) );
            $src_x = round( ( $width - ( $width / $cmp_x * $cmp_y ) ) / 2 );

        } elseif ( $cmp_y > $cmp_x ) {

            $src_h = round( ( $height / $cmp_y * $cmp_x ) );
            $src_y = round( ( $height - ( $height / $cmp_y * $cmp_x ) ) / 2 );

        }
        
        imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h );

    } else {

        // copy and resize part of an image with resampling
        imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

    }
        
    // output image to browser based on mime type
    show_image( $mime_type, $canvas, $quality, $cache_dir );
    
    // remove image from memory
    imagedestroy( $canvas );
    
} else {

    if(strlen($src)) {
        die($src . ' not found.');
    } else {
        die('no source specified.');
    }
    
}

function show_image( $mime_type, $image_resized, $quality, $cache_dir ) {

    // check to see if we can write to the cache directory
    $is_writable = 0;
    $cache_file_name = $cache_dir . '/' . get_cache_file();            

    if(touch($cache_file_name)) {
    
        // give 666 permissions so that the developer 
        // can overwrite web server user
        chmod($cache_file_name, 0666);
        $is_writable = 1;
        
    } else {
    
        $cache_file_name = NULL;
        header('Content-type: ' . $mime_type);
        
    }
    
    if(stristr($mime_type, 'gif')) {
    
        imagegif($image_resized, $cache_file_name);
        
    } elseif(stristr($mime_type, 'jpeg')) {
    
        imagejpeg($image_resized, $cache_file_name, $quality);
        
    } elseif(stristr($mime_type, 'png')) {
    
        $quality = floor($quality * 0.09);        
        imagepng($image_resized, $cache_file_name, $quality);
        
    }
    
    if($is_writable) {
        show_cache_file( $cache_dir, $mime_type );
    }

    die();

}

function get_request( $property, $default = 0 ) {
    
    if( isset($_REQUEST[$property]) ) {
        return $_REQUEST[$property];
    } else {
        return $default;
    }
    
}

function open_image($mime_type, $src) {

    if(stristr($mime_type, 'gif')) {
    
        $image = imagecreatefromgif($src);
        
    } elseif(stristr($mime_type, 'jpeg')) {
    
        @ini_set('gd.jpeg_ignore_warning', 1);
        $image = imagecreatefromjpeg($src);
        
    } elseif( stristr($mime_type, 'png')) {
    
        $image = imagecreatefrompng($src);
        
    }
    
    return $image;

}

function mime_type($file) {

    $os = strtolower(php_uname());
    $mime_type = '';

    // use PECL fileinfo to determine mime type
    if( function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME);
        $mime_type = finfo_file($finfo, $file);
        finfo_close($finfo);
    }

    // try to determine mime type by using unix file command
    // this should not be executed on windows
    if(!valid_src_mime_type($mime_type) && !(eregi('windows', $os))) {
        if(preg_match("/freebsd|linux/", $os)) {
            $mime_type = trim(@shell_exec('file -bi $file'));
        }
    }

    // use file's extension to determine mime type
    if(!valid_src_mime_type($mime_type)) {

        // set defaults
        $mime_type = 'image/jpeg';
        // file details
        $fileDetails = pathinfo($file);
        $ext = strtolower($fileDetails["extension"]);
        // mime types
        $types = array(
             'jpg'  => 'image/jpeg',
             'jpeg' => 'image/jpeg',
             'png'  => 'image/png',
             'gif'  => 'image/gif'
         );
        
        if(strlen($ext) && strlen($types[$ext])) {
            $mime_type = $types[$ext];
        }
        
    }
    
    return $mime_type;

}

function valid_src_mime_type($mime_type) {

    if(preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
        return true;
    }
    return false;

}

function check_cache($cache_dir, $mime_type) {

    // make sure cache dir exists
    if(!file_exists($cache_dir)) {
        // give 777 permissions so that developer can overwrite
        // files created by web server user
        mkdir($cache_dir);
        chmod($cache_dir, 0777);
    }

    show_cache_file($cache_dir, $mime_type);

}

function show_cache_file($cache_dir, $mime_type) {

    $cache_file = $cache_dir . '/' . get_cache_file();

    if( file_exists( $cache_file ) ) {
        
        if( isset( $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] ) ) {
        
            // check for updates
            $if_modified_since = preg_replace( '/;.*$/', '', $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] );                    
            $gmdate_mod = gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) );
            
            if( strstr( $gmdate_mod, 'GMT' ) ) {
                $gmdate_mod .= " GMT";
            }
            
            if ( $if_modified_since == $gmdate_mod ) {
                header( "HTTP/1.1 304 Not Modified" );
                exit;
            }

        }
        
        $fileSize = filesize($cache_file);
                
        // send headers then display image
        header("Content-Type: " . $mime_type);
        //header("Accept-Ranges: bytes");
        header("Last-Modified: " . gmdate('D, d M Y H:i:s', filemtime($cache_file)) . " GMT");
        header("Content-Length: " . $fileSize);
        header("Cache-Control: max-age=9999, must-revalidate");
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + 9999) . "GMT");
        
        readfile($cache_file);
        
        die();

    }
    
}

function get_cache_file () {

    global $quality;

    static $cache_file;
    if(!$cache_file) {
        $frags = split( "\.", $_REQUEST['src'] );
        $ext = strtolower( $frags[ count( $frags ) - 1 ] );
        if(!valid_extension($ext)) { $ext = 'jpg'; }
        $cachename = get_request( 'src', 'timthumb' ) . get_request( 'w', 100 ) . get_request( 'h', 100 ) . get_request( 'zc', 1 ) . get_request( '9', 80 );
        $cache_file = md5( $cachename ) . '.' . $ext;
        
    }
    return $cache_file;

}

function valid_extension ($ext) {

    if( preg_match( "/jpg|jpeg|png|gif/i", $ext ) ) return 1;
    return 0;

}

function clean_source ( $src ) {

    // remove http/ https/ ftp
    $src = preg_replace("/^((ht|f)tp(s|):\/\/)/i", "", $src);
    // remove domain name from the source url
    $host = $_SERVER["HTTP_HOST"];
    $src = str_replace($host, "", $src);
    $host = str_replace("www.", "", $host);
    $src = str_replace($host, "", $src);
    
    //$src = preg_replace( "/(?:^\/+|\.{2,}\/+?)/", "", $src );
    //$src = preg_replace( '/^\w+:\/\/[^\/]+/', '', $src );

    // don't allow users the ability to use '../' 
    // in order to gain access to files below document root

    // src should be specified relative to document root like:
    // src=images/img.jpg or src=/images/img.jpg
    // not like:
    // src=../images/img.jpg
    $src = preg_replace( "/\.\.+\//", "", $src );

    return $src;

}

function get_document_root ($src) {
    if( @file_exists( $_SERVER['DOCUMENT_ROOT'] . '/' . $src ) ) {
        return $_SERVER['DOCUMENT_ROOT'];
    }
    // the relative paths below are useful if timthumb is moved outside of document root
    // specifically if installed in wordpress themes like mimbo pro:
    // /wp-content/themes/mimbopro/scripts/timthumb.php
    $paths = array( '..', '../..', '../../..', '../../../..' );
    foreach( $paths as $path ) {
        if( @file_exists( $path . '/' . $src ) ) {
            return $path;
        }
    }
    


}

?>


 
Odgovor na temu

amitkic
student
zvechan

Član broj: 186610
Poruke: 42
82.114.95.*



Profil

icon Re: Pametan thumbnail - kako?19.07.2009. u 08:26 - pre 179 meseci
Code:


$upload_path='../images/';
$add=$upload_path.$_FILES['userfile']['name'];
         $mw = 120; // max width
        $mh = 90; // max height
        $rw=130;
        $rh=100;
        $im=ImageCreateFromJPEG($add);
        $width=ImageSx($im);
        $height=ImageSy($im);
        if( $width > $mw || $height > $mh ) {
            if( $width > $height ) {
            $tnw = $mw;
            $tnh = $tnw * $height / $width;
        } else {
            $tnh = $mh;
            $tnw = $tnh * $width / $height;
            }
        } else {
        $tnw = $ow;
        $tnh = $oh;
            }
        $newimage=imagecreatetruecolor($rw,$rh);
        ImageCopyResized($newimage,$im,0,0,0,0,$thw,$tnh,$width,$height);
        ImageGIF($newimage,$tsrc);


 
Odgovor na temu

Šahbaz
Developer, BBM
EU

Član broj: 102142
Poruke: 106
95.155.20.*



Profil

icon Re: Pametan thumbnail - kako?24.07.2009. u 15:05 - pre 179 meseci
Hvala svima,

Iskoristio sam kod koji je ostavio _CaRtMan_
 
Odgovor na temu

[es] :: PHP :: Pametan thumbnail - kako?

[ Pregleda: 2183 | Odgovora: 8 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.