function createThumbnail

zanni79

Nuovo Utente
23 Lug 2010
3
0
0
salve a tutti ,
vorrei un consiglio aproposito della funzione php create thumbnail . Sto cercando di aggiungere la variabile percentuale per la creazione delle miniature ma non riesco assolutamente ad uscirne .

Il codice in questione è questo :

}
function _createThumbnail($fileName, $ext, $tw, $th, $newname, $thumbname) {
$mainimage = './uploads/'.$fileName.'.'.$ext;
$t = './uploads/'.$fileName.'_thumb'.'.'.$ext;

list($width, $height, $type, $attr) = getimagesize($mainimage);

$image_width = $width;
$image_height = $height;
$image_type = $type;

$scale = 0.3;
if($image_width > $image_height) {
$final_width = $tw;
$final_height = $th;
} elseif($image_width == $image_height) {
$final_width = $tw;
$final_height = $th;
} else {
$final_width = $tw;
$final_height = $th;
}

$x = $final_width/$image_width;
$y = $final_height/$image_height;

if($x < $y) {
$new_width = round($image_width *($final_height/$image_height));
$new_height = $final_height;
} else {
$new_height = round($image_height *($final_width/$image_width));
$new_width = $final_width;
}
$to_crop_left = ($new_width - ($final_width *$scale))/2;
$to_crop_top = ($new_height - ($final_height *$scale))/2;

$config['image_library'] = 'GD2';
$config['source_image'] = $mainimage;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = true;
$config['master_dim'] = 'width';
$config['width'] = $new_width;
$config['height'] = $new_height;
$config['quality'] = '100%';

$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();

$config['image_library'] = 'GD2';
$config['source_image'] = $t;
$config['create_thumb'] = FALSE;
$config['width'] = $final_width;
$config['height'] = $final_height;
$config['x_axis'] = $to_crop_left;
$config['y_axis'] = $to_crop_top;
$config['maintain_ratio'] = false;

$this->image_lib->initialize($config);
if(!$this->image_lib->crop()) {
echo $this->image_lib->display_errors();
} else {
$this->gallery_tbl->insertImage($_POST['id'], $newname, $thumbname);
}
}
function removeimage() {
$data['info'] = $this->gallery_tbl->particularImage($this->uri->segment(3));
$this->load->view('image_delete', $data);
}
function do_remove() {
$full = $this->gallery_tbl->imgToDelete($this->uri->segment(3));
$thumb = $this->gallery_tbl->thumbToDelete($this->uri->segment(3));
if(isset($full)) unlink('./uploads/'.$full);
if(isset($thumb)) unlink('./uploads/'.$thumb);
$this->gallery_tbl->deleteImage();
}
}

Aspetto una risposta e vi ringrazio in anticipo.
 

Eliox

Utente Attivo
25 Feb 2005
4.390
3
0
ti può servire una funzione del genere?
PHP:
 function imageResize($width,$height,$max) {
  if ($width>$height) $percent=($max/$width);
  else $percent=($max/$height);
  $width=round($width*$percent);
  $height=round($height*$percent);
  return $width."|".$height;
 }
 

zanni79

Nuovo Utente
23 Lug 2010
3
0
0
re-thumbnail

grazie tante per la risposta ...
ho provato ad integrare il mio codice ma non funziona....ammetto che non sono esperto di php anzi sono all'inizio.

come posso fare ?

grazie ancora
 

zanni79

Nuovo Utente
23 Lug 2010
3
0
0
re thumb

grazie Eliox per la pazienza... ti posto il codice anche se credo che il problema non sia tanto semplice.... questo è il codice che fa parte della gallerycms... un csm opensource per il caricamento di immagini ... dovrei anche disattavare l'input dato dalla pagina setting ( con il quale puoi impostare la larghezza e altezza della thumb) perchè vorrei che il programma creasse le thumbinail in percentuale alle immagini originali, una opercentuale fissa per tutte.


<?php
class Gallery extends Controller {
function Gallery() {
parent::Controller();
$this->load->model('gallery_tbl');
$loggedin = $this->session->userdata('userid');
if (!isset($loggedin) or $loggedin=='') redirect('login');
}
function index() {
$data['rows'] = $this->gallery_tbl->viewCategories();
$this->load->view('gallery_view', $data);
}
function view() {
$data['rows'] = $this->gallery_tbl->viewParticular($this->uri->segment(3));
$this->load->view('category_view_id', $data);
}
function add() {
$this->load->view('category_insert');
}
function insert() {
$this->gallery_tbl->insertCategory();
}
function update() {
$data['rows'] = $this->gallery_tbl->viewParticular($this->uri->segment(3));
$this->load->view('category_update', $data);
}
function updatecat() {
$this->gallery_tbl->updateCategory();
}
function delete() {
$data['rows'] = $this->gallery_tbl->viewParticular($this->uri->segment(3));
$this->load->view('category_delete', $data);
}
function deleterow() {
$this->gallery_tbl->deleteCategory();
}
function images() {
$data['info'] = $this->gallery_tbl->viewParticular($this->uri->segment(3));
$data['rows'] = $this->gallery_tbl->showImages($this->uri->segment(3));
$this->load->view('image_view', $data);
}
function addimage() {
$data['info'] = $this->gallery_tbl->viewParticular($this->uri->segment(3));
$this->load->view('image_insert', $data);
}
function updateimage() {
$data['rows'] = $this->gallery_tbl->particularImage($this->uri->segment(3));
$this->load->view('image_update', $data);
}
function do_updateimage() {
$this->gallery_tbl->updateImage();
}
function do_upload() {
ini_set("memory_limit","48M");
$this->load->model('settings_tbl');
$data['settings'] = $this->settings_tbl->viewSettings();
$thumbWidth = $data['settings'][0]->thumb_width;
$thumbHeight = $data['settings'][0]->thumb_height;
if (isset($_FILES['filename'])) {
$file = read_file($_FILES['filename']['tmp_name']);
$maxfilesize = 2000000;
$uploadsize = ceil($_FILES['filename']['size']);

if ($uploadsize > $maxfilesize || $uploadsize<5 || !isset($uploadsize)) {
redirect('gallery/images/'.$_POST['id']);
exit();
}

$fname = $_FILES['filename']['name'];
$ext = strtolower(substr($fname, strrpos($fname,'.')+1));
$md5Date = Date("Y_m_d_H_i_s");
$newname = $md5Date.'.'.$ext;
$thumbname = $md5Date.'_thumb.'.$ext;
$name = basename($_FILES['filename']['name']);

if ($ext!="jpg"&&$ext&&"jpeg"&&$ext!="png"&&$ext!="gif") {
redirect('gallery/images/'.$_POST['id']);
exit();
}
$uploadImage = './uploads/'.$newname;
write_file($uploadImage, $file);
$this->_createThumbnail($md5Date, $ext, $thumbWidth, $thumbHeight, $newname, $thumbname);
} else {
$this->load->view('gallery_view');
}
}
function _createThumbnail($fileName, $ext, $tw, $th, $newname, $thumbname) {
$mainimage = './uploads/'.$fileName.'.'.$ext;
$t = './uploads/'.$fileName.'_thumb'.'.'.$ext;

list($width, $height, $type, $attr) = getimagesize($mainimage);

$image_width = $width;
$image_height = $height;


$scale = 1.0;
if($image_width > $image_height) {
$final_width = $tw;
$final_height = $th;
} else {
$final_width = $tw;
$final_height = $th;
}

$x = $final_width/$image_width;
$y = $final_height/$image_height;

$new_width=(int)$final_width*100/60; //Larghezza immagine ridimensionata
$new_height=(int)$final_height*100/60; //Altezza immagine ridimensionata


$to_crop_left = ($new_width - ($final_width *$scale))/2;
$to_crop_top = ($new_height - ($final_height *$scale))/2;


$config['image_library'] = 'GD2';
$config['source_image'] = $mainimage;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = true;
$config['master_dim'] = 'width';
$config['width'] = $new_width;
$config['height'] = $new_height;
$config['quality'] = '100%';

$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();

$config['image_library'] = 'GD2';
$config['source_image'] = $t;
$config['create_thumb'] = FALSE;
$config['width'] = $final_width;
$config['height'] = $final_height;
$config['x_axis'] = $to_crop_left;
$config['y_axis'] = $to_crop_top;
$config['maintain_ratio'] = false;

$this->image_lib->initialize($config);
if(!$this->image_lib->crop()) {
echo $this->image_lib->display_errors();
} else {
$this->gallery_tbl->insertImage($_POST['id'], $newname, $thumbname);
}
}
function removeimage() {
$data['info'] = $this->gallery_tbl->particularImage($this->uri->segment(3));
$this->load->view('image_delete', $data);
}
function do_remove() {
$full = $this->gallery_tbl->imgToDelete($this->uri->segment(3));
$thumb = $this->gallery_tbl->thumbToDelete($this->uri->segment(3));
if(isset($full)) unlink('./uploads/'.$full);
if(isset($thumb)) unlink('./uploads/'.$thumb);
$this->gallery_tbl->deleteImage();
}
}
 
Discussioni simili
Autore Titolo Forum Risposte Data
pacemattia408 disattivazione di una function Javascript 4
W Microsoft OLE DB Provider for Visual FoxPro error '80040e14' Function name is missing ). Classic ASP 0
L [PHP] Fatal error: Call to a member function prepare() on null in PHP 0
A [PHP] public static function isEan13 PHP 3
M leggere con jquery/ajax in una function javascript record di database sql server Javascript 0
Y [Javascript] suggerimenti su utilizzo onreadystatechange = function() Javascript 7
Cosina Link in document ready function jQuery 0
C [PHP] Errore "Fatal error: Call to undefined function getTotalUsers()" PHP 2
B mysql_connect() [function.mysql-connect]: Access denied for user... PHP 13
bubino8 [Javascript] Problema function eseguita solo la prima volta Javascript 1
G [PHP] Problema - Warning: session_start() [function.session-start] PHP 9
G [RISOLTO][PHP] Call to a member function num_rows() on a non-object PHP 9
A Errore PHP: Call to a member function on null PHP 5
V Jquery function find jQuery 7
U [PHP] Fatal error: Call to a member function Query() PHP 1
D Perchè la function non funziona? Javascript 0
F Output html function jQuery 1
O Fatal error: Call to undefined function testNome() in C:\xampp\...\...\index.php on line 51 PHP 4
JackIlPazzo Fatal error: Call to a member function execute() on a non-object PHP 2
felino [JQuery] TypeError: $ is not a function jQuery 1
filippino Fatal error: Cannot redeclare (function) PHP 2
P Call to a member function bind_param() on a non-object PHP 5
JackIlPazzo PHP: Fatal error: Call to a member function bind_param() on a non-object PHP 0
M Function - Class + OOP Javascript 1
K mail() [function mail]: failed to connect to mailserver at localhost port 25 PHP 1
F [RISOLTO]js function per impostare minimo totale Javascript 8
F problemi con $.each(data, function(i,item) e getElementById("livello").innerHTML= Javascript 0
P Call to a member function Send() on a non-object PHP 6
ivarello Function e variabili??? PHP 12
M fatal error: Call to undefined function gdrcd_filter() PHP 6
A function conferma Javascript 6
M scope callback function Javascript 1
S Help : Warning: mail() [function.mail] PHP 2
A Navigazione jQuery [era: $(document).ready(function(){] jQuery 3
C Colorare i button con una function Javascript 35
T Fatal error: Call to a member function show_crom() on a non-object PHP 1
G Problema [function mail] Aruba Hosting 0
V Function ed Array PHP 7
F Function session_is_registered() is deprecated PHP 16
A inserire la function orologio() in un div Javascript 12
C Function auto completamento campo zipcode PHP 3
C Inserimento key in una function strurl Javascript 0
B function con query non funzionante PHP 3
F Fatal error: Call to undefined function PHP 9
C Come richiamare una function di controllo Javascript 0
ciacos74 PHP errore function.session-start PHP 25
C Spedire via Mail il risultato di una Function CMS (Content Management System) 3
catellostefano smarty assign function PHP 1
D Errori php 4 es: Function ereg() is deprecated PHP 2
A Function.strpos Javascript 3

Discussioni simili