← PHP

Problema: Script de redimensionamento de imagens

Lida 3445 vezes

Offline

Celso Azevedo 
Membro
Mensagens 3500 Gostos 38
Feedback +12

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Tenth year Anniversary Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Olá, estou a tentar utilizar um script que redimensione imagens e que guarde a imagem pequena.

» Encontrei isto: http://mrphp.com.au/code/project/miscellaneous/image-cache-using-phpthumb-and-htaccess
» Este script utiliza também o phpThumb: http://phpthumb.sourceforge.net/

Eu já...
... coloquei o phpThumb no alojamento com o nome (pasta) phpthumb,
... criei a pasta site.com/thumbs,
... criei o ficheiro .htaccess (e coloquei dentro da pasta thumbs) com o seguinte código:
Código: [Seleccione]
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?thumb=$1 [L,QSA]
</IfModule>
... criei o ficheiro index.php (e coloquei dentro da pasta thumbs) com o seguinte código:
Código: [Seleccione]
/**
 * Create a thumbnail
 *
 * @author Brett @ Mr PHP
 */
 
// define allowed image sizes
$sizes = array(
        '293x150',
        '100x100',
        '250x250',
);

// ensure there was a thumb in the URL
if (!$_GET['thumb']) {
        error('no thumb');
}

// get the thumbnail from the URL
$thumb = strip_tags(htmlspecialchars($_GET['thumb']));

// get the image and size
$thumb_array = explode('.',$thumb);
$image = '../';
foreach($thumb_array as $k=>$thumb_part){
        if ($k != count($thumb_array)-2) {
                $image .= $thumb_part . '.';
        }
}
$image = substr($image,0,-1);
$size = $thumb_array[count($thumb_array)-2];
list($width,$height) = explode('x',$size);

// ensure the size is valid
if (!in_array($size,$sizes)) {
        error('invalid size');
}

// ensure the image file exists
if (!file_exists($image)) {
        error('no source image');
}

// generate the thumbnail
require('../phpthumb/phpthumb.class.php');
$phpThumb = new phpThumb();
$phpThumb->setSourceFilename($image);
$phpThumb->setParameter('w',$width);
$phpThumb->setParameter('h',$height);
//$phpThumb->setParameter('far','C'); // scale outside
//$phpThumb->setParameter('bg','FFFFFF'); // scale outside
if (!$phpThumb->GenerateThumbnail()) {
        error('cannot generate thumbnail');
}

// make the directory to put the image
if (!mkpath(dirname($thumb),true)) {
        error('cannot create directory');
}

// write the file
if (!$phpThumb->RenderToFile($thumb)) {
        error('cannot save thumbnail');
}

// redirect to the thumb
// note: you need the '?new' or IE wont do a redirect
header('Location: /thumbs/'.$thumb.'?new');

// basic error handling
function error($error) {
        header("HTTP/1.0 404 Not Found");
        echo '<h1>Not Found</h1>';
        echo '<p>The image you requested could not be found.</p>';
        echo "<p>An error was triggered: <b>$error</b></p>";
        exit();
}
//recursive dir function
function mkpath($path, $mode){
    is_dir(dirname($path)) || mkpath(dirname($path), $mode);
    return is_dir($path) || @mkdir($path,0777,$mode);
}

Pelo que percebi, as imagens tinham que ser colocadas numa pasta chamada de images e para 'chamar' a imagem pequena tenho que utilizar yoursite.com/thumbs/images/myimage.100x100.jpg.

A imagem de teste é esta: http://www.wallshd.com/images/look-what-i-ate.jpg

Segundo o que diz no site, tinha que conseguir visualiza-la por este link: http://www.wallshd.com/thumbs/images/look-what-i-ate.293x150.jpg (mas não funciona). Tirei também o 'images' (/thumbs/images/look-what-i-ate.293x150.jpg) e também não funciona...

Eu coloquei, para testar, todas as pastas e ficheiros necessários com a permissão '777' e mesmo assim não funciona.

Alguém sabe onde está o erro?

Obrigado.
Offline

Celso Azevedo 
Membro
Mensagens 3500 Gostos 38
Feedback +12

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Tenth year Anniversary Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Alguém ?  :-?
Offline

Celso Azevedo 
Membro
Mensagens 3500 Gostos 38
Feedback +12

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Tenth year Anniversary Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Já resolvi :)
Offline

Henrique Mouta 
Equipa
Mensagens 1189 Gostos 3
Troféus totais: 31
Trófeus: (Ver todos)
Level 6 Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3

Explica o que fizeste :)
Offline

Celso Azevedo 
Membro
Mensagens 3500 Gostos 38
Feedback +12

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Tenth year Anniversary Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Fiz uma operação ao tema e fiz o transplante de um pedaço do tema que utilizo no www.iphonenwalls.com :D