Vorrei creare uno script php che rimpicciolisce le immagini creando delle iconcine automaticamente. E' possibile?
Grazie
Printable View
Vorrei creare uno script php che rimpicciolisce le immagini creando delle iconcine automaticamente. E' possibile?
Grazie
Ti consiglio questo che ho fatto io e mi sembra piuttosto funzionale:
qui ci sono le istruzione per farlo funzionare a dovere.Codice PHP:<?php
/*
* PHP RESIZER WIZARD
* Versione: 1.1
* Licenza: GPL
* Autore: Tuttoeniente
* Contributi: Heracleum, Forum di AlterVista.org
*/
// *** CONFIGURAZIONE ***//
$onthefly = 0; //Generazione al volo delle immagini, cache disattiva: 1 = si 0 = no.
//ATTENZIONE: Con questa opzione attivata il caricamento delle pagine con tante immagini potrebbe risultare
//visibilmente rallentato. Usare questa opzione è consigliabile solo in fase di sviluppo, in caso di necessità di non
//occupare spazio sul server, o nel caso non si disponga di permessi di scrittura
//N.B.: Se è disattivata, qualsiasi dimensione, watermark, rotazione od altro passati non verranno eseguiti, verrà
//solo caricata la versione della cache
$defsize = 300; //Larghezza/altezza predefinita del thumbnail
$resample = 1; //Utilizzo della funzione resample nella riduzione, che garantisce una migliore qualità ma è più lento (solo nella
//generazione delle immagini, non nel richiamo dalla cache): 1 = si 0 = no
$persfont = 0; //Font .ttf personalizzato per il watermark: 1 = si (necessario inserire quale nella prossima variabile) 0 = no
$font = ""; //Nome del file del font, relativo alla directory dello script
$size = 18; //Dimensione del font (solo se personalizzato)
$alwayswm = 0; //Watermark su tutte le immagini: 1 = si 0 = no
$defwm = ""; //Testo predefinito del watermark, obbligatorio se alwayswm è settato a 1
$trasp = 110; //Trasparenza del watermark, da 0 a 127
//*** FINE CONFIGURAZIONE ***//
$imgfile = htmlspecialchars(@$_GET['file']);
$size = @$_GET['size'];
$rot = @$_GET['rot'];
$wm = @$_GET['wm'];
$al = @$_GET['al'];
$ext = explode(".", $imgfile);
$n = count($ext)-1;
$ext = strtolower($ext[$n]);
$get = '';
foreach($_GET as $k => $v){
$get .= $k.$v;
}
$nomethumb = "thumb/thumb_".md5($imgfile.$get).".".$ext;
function read($f){
global $ext,$imgfile,$nomethumb;
if($ext == "jpg" || $ext == "jpeg"){
header('Content-type: image/jpeg');
}
elseif($ext == "gif"){
header('Content-type: image/gif');
}
elseif($ext == "png"){
header('Content-type: image/png');
}
elseif($ext == "bmp"){
header('Content-type: image/vnd.wap.wbmp');
}
$handle = fopen($nomethumb, "r");
echo fread($handle, filesize($nomethumb));
fclose($handle);
}
function error($e){
$w = (imagefontwidth(5) * strlen($e));
$h = imagefontheight(5);
$img = imagecreate($w,$h);
$bgcolor = imagecolorallocate($img, 255, 255, 255);
$txtcolor = imagecolorallocate($img, 255, 0, 0);
imagestring($img, 5, 0, 0, $e, $txtcolor);
header("Content-type: image/gif");
imagegif($img);
}
function resize($source){
global $resample, $thumb, $newwidth, $newheight, $width, $height;
if($resample == 1){
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
return $thumb;
}
function edit($thumb){
global $ext, $imgfile, $rot, $source, $newheight, $newwidth, $wm, $al, $persfont, $font, $alwayswm, $defwm, $size, $tras;
if(isset($rot)){
if(is_numeric($rot)){
$thumb = imagerotate($thumb, $rot, 0);
}
}
if(isset($wm) || $alwayswm == 1){
$black = imagecolorallocatealpha($thumb, 0, 0, 0, $tras);
if($alwayswm == 1){
$wm = $defwm;
}
if($persfont == 0){
$font = 5;
}
if(!isset($al) || ($al != "left" && $al != "center" && $al != "right")){
$al = "right";
}
switch ($al){
case "left":
$w = 2;
break;
case "center":
$w = ($newwidth - (strlen($wm) * imagefontwidth($font))) / 2;
break;
case "right":
if($persfont == 1){
$w = $newwidth - (strlen($wm) * (imagefontwidth($font) * $size/2)) - 2;
}else{
$w = $newwidth - (strlen($wm) * imagefontwidth($font)) - 2;
}
break;
}
$h = $newheight - imagefontheight(5) - 2;
if($persfont == 0 || trim($font) == ""){
imagestring($thumb, 5, $w, $h, $wm, $black);
}else{
imagefttext($thumb, $size, 0, $w, $h, $black, $font, $_GET['wm']);
}
}
return $thumb;
}
if($alwayswm == 1 && trim($defwm) == ""){
error("ERRORE: Necessario watermark predefinito con il watermark obbligatorio attivato");
}
if(!is_dir("thumb")){
mkdir("thumb");
}
if(!is_file($imgfile)){
error("ERRORE: L'immagine non esiste");
die();
}
if(!isset($size)){
$size = $defsize;
}
if(is_file($nomethumb) && $onthefly == 0){
read($nomethumb);
die();
}
if(!is_file($imgfile)){
error("ERRORE: Il file non esiste");
die();
}
list($width, $height) = getimagesize($imgfile);
//Heracleum mod
if($height < $width){
$newwidth = $size;
$rap = $height / $width;
$newheight = $newwidth * $rap;
}else{
$newheight = $size;
$rap = $width / $height;
$newwidth = $newheight * $rap;
}
$thumb = ImageCreateTrueColor($newwidth,$newheight);
imageantialias($thumb,1);
if($ext == "jpg" || $ext == "jpeg"){
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($imgfile);
$thumb = resize($source);
$thumb = edit($thumb);
if($onthefly == 1){
imagejpeg($thumb);
}else{
imagejpeg($thumb, $nomethumb);
read($nomethumb);
}
}
elseif($ext == "gif"){
header('Content-type: image/gif');
$source = imagecreatefromgif($imgfile);
$thumb = resize($source);
$thumb = edit($thumb);
if($onthefly == 1){
imagegif($thumb);
}else{
imagegif($thumb, $nomethumb);
read($nomethumb);
}
}
elseif($ext == "png"){
header('Content-type: image/png');
$source = imagecreatefrompng($imgfile);
$thumb = resize($source);
$thumb = edit($thumb);
if($onthefly == 1){
imagepng($thumb);
}else{
imagepng($thumb, $nomethumb);
read($nomethumb);
}
}
elseif($ext == "bmp"){
header('Content-type: image/vnd.wap.wbmp');
$source = imagecreatefrompng($imgfile);
$thumb = resize($source);
$thumb = edit($thumb);
if($onthefly == 1){
imagewbmp($thumb);
}else{
imagewbmp($thumb, $nomethumb);
read($nomethumb);
}
}else{
error("ERRORE: Formato non supportato");
die();
}
?>
ho provato i primi due script ma i seguenti comandi non mi vengono riconosciuti da php:
imagecreatefromjpeg($imgname_in);
imagecreatetruecolor($nw,$nh);
:sad: :sad:
Io aggiungo sempre questo prima dello script perchè a volte mi da errore:
Ciao!Codice PHP:<?
header("content-type: image/jpeg");
if (!extension_loaded('gd')) {
dl('php_gd2.dll');
}
...
?>
Ecco il mio codice, che continua a non funzionare...
Se riuscite a darmi una mano...vi sarei grato
Codice PHP:<?
header("content-type: image/jpeg");
if (!extension_loaded('gd')) {
dl('php_gd2.dll');
}
$imgname_in="foto1.jpg";
$imgname_out="foto1_thumb.jpg";
$dim=getimagesize($imgname_in);
$w=$dim[0];
$h=$dim[1];
// ridimensiona l'immagine con larghezza 100
// e altezza in proporzione
$nw=100;
$percentuale = $nw/$w;
$nh=$h*$percentuale;
$im=imagecreatefromjpeg($imgname_in);
$out=imagecreatetruecolor($nw,$nh);
imagecopyresized($out,$im, 0, 0, 0, 0, $nw,$nh,$w,$h);
imagejpeg($out,$imgname_out);
imagedestroy($out);
?>
Non so se può risolvere, ma al posto di getimagesize usa imagesx e imagesy che restituisce larghezza e altezza di un immagine..
EDIT: Ho provato lo script e funziona se alla fine metti
al posto diCodice PHP:imagejpeg($out);
ciao!Codice PHP:imagejpeg($out,$imgname_out);
io uso un Mac e in locale mi da errore...
Questo è il codice che ho trovato nel sorgente della pagina. Invece il browser mi visualizza solo il path del file che sto eseguendoCodice:<br />
<b>Warning</b>: dl(): Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20020429/php_gd2.dll' - (null) in <b>/Users/emac/Sites/image.php</b> on line <b>3</b><br />
<br />
<b>Fatal error</b>: Call to undefined function: imagesx() in <b>/Users/emac/Sites/image.php</b> on line <b>7</b><br />
Codice:http://localhost/~emac/image.php
ma hai abilitato l'estensione gd nel php.ini?
se non l'hai attivata devi cercare appunto il file php.ini, cerca "Extension" e togli il ; davanti a php_gd2.dll..
Altrimenti carica il file su AV e vedi se funziona..
Ciao!
A quanto vedo sta eseguendo lo script su linux, e se non sbaglio lì le estensioni del php sono .so non .dll