Problema creazione thumb solo con alcune immagini

  • Creatore Discussione Creatore Discussione LaKanka
  • Data di inizio Data di inizio

LaKanka

Utente Attivo
29 Ago 2008
166
0
0
Ciao a tutti, ho un problema con delle immagini che ho già ridotte (altezza max 110 e larghezza max 110) mantenendo le proporzioni, e che devo sovrapporre centralmente a delle immagini con sfondo bianco 110x110.
Il codice sembrava mi funzionasse perfettamente con le prime prove, ma con alcune foto sembra che l'immagine che creo sia opaca, ha come una patina grigia (alcune grigio chiaro altre grigo scuro) su tutta la foto....
Non so proprio cosa possa essere...

Il codice
PHP:
function createThumbnail_vetrina($img)
{ 
  $size = getimagesize("../thumb/".$img);
  $width = $size[0];
  $height = $size[1];
  if( $width < 110 ) $x = (110 - $width) / 2 ;
  if( $height < 110 ) $y = (110 - $height) / 2 ;
  // Resample the image.
  $im_sfondo = @imagecreate(110, 110) or die("Cannot Initialize new GD image stream");
  $im_sfondo = imagecolorallocate($im_sfondo, 255, 255, 255);
 $dest = imagecreate(110, 110) or die("errore nel caricare gd");
$sfondo = imagecreatefromjpeg($im_sfondo);
$icona = imagecreatefromjpeg('../thumb/'.$img);
imagecopy($dest, $sfondo, 0, 0, 0, 0, 110, 110);
imagecopy($dest, $icona, $x, $y, 0, 0, $width, $height);
$newName="../vetrina/".$img;
imagejpeg($dest,$newName,100);
$newName="vetrina/".$img;
return $newName;
}

La variabile $img contiene solo thumb jpg che ho creato da immagini di vario formato (gif,jpg,ecc..)

Non capisco quale possa essere il problema..:dipser:
 
Anche io ho avuto lo stesso problema, tempo fa, e non ho mai risolto. Qualcuno se non sbaglio mi aveva detto che era un problema dovuto alle tonalità di colori presenti in quella particolare immagine (calcola che il mio era un crea-targhette, quindi le immagini erano in tinta unita).
 
Effettivamente ora che ci penso... andava tutto bene finchè caricavo immagini provenienti da scannerizzazioni o disegni con poche tonalità di colori, mentre quel problema me lo da solo con le foto che hanno molte più tonalità.....
Ma quello che mi dici non è che mi rassicuri molto!!!
Non può non esserci una soluzione al problema... ho bisogno di queste thumb!!!:dipser:
 
prova ad usare questa funzione, è quella che utilizzo sempre e non ho mai avuto i problemi che segnali (oppure non mi è ancora capitato :)):
PHP:
<?php
function getThumb($weight_tb, $height_tb, $source)
  {
$img = @imagecreatefromjpeg($source);
  $weight = @imagesx($img);
  $height = @imagesy($img);
  $ratio = @min($weight_tb/$weight, $height_tb/$height);
  
if ($ratio < 1)
  {
  $new_weight = @floor($ratio*$weight);
  $new_height = @floor($ratio*$height);
  $temp = @imagecreatetruecolor($new_weight, $new_height);
  @imagecopyresized($temp, $img,0,0,0,0,
  $new_weight, $new_height, $weight, $height);
  @imagedestroy($img);
  $img = $temp;
  }
  
@header("Content-type: image/jpeg");
  $img_res = @imagejpeg($img);
  return $img_res;
}
?>
 
Bè le thumb iniziali le creo anch'io senza problemi.

Lo script che ho postato sopra incolla un immagine sopra un'altra ed è questo che mi sballa la tonalità o luminosità..

Quando carico le foto creo delle thumb di ciascuna utilizzando questa funzione che non mi dà alcun problema:
PHP:
function createThumbnail($img, $imgPath, $suffix, $newWidth, $newHeight, $quality, $newName)
{ $size = getimagesize($img);
  if( $size[2] == 2 ){$original = @imagecreatefromjpeg($img);}
  elseif( $size[2] == 1 ){$original = @imagecreatefromgif($img);}
  elseif( $size[2] == 3 ){$ioriginalm = @imagecreatefrompng($img);}

  $newwidth = $size[0];
  $newheight = $size[1];
  
  if( $newwidth > $newWidth ){
    $newheight = ($newWidth / $newwidth) * $newheight;
    $newwidth = $newWidth;
  }
  if( $newheight > $newHeight ){
    $newwidth = ($newHeight/ $newheight) * $newwidth;
    $newheight = $newHeight;
  }

  // Resample the image.
  $tempImg = imagecreatetruecolor($newwidth, $newheight) or die("Cant create temp image");
imagecopyresampled ($tempImg, $original, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1])or die("Cant resize copy");
  // Save the image.
 imagejpeg($tempImg, $newName, 100)or die("Cant save image");
  // Clean up.
  imagedestroy($tempImg);
  return $newName;
}
Non mi ha dato alcun problema...
Dopodichè posso decidere di mettere quella thumb nella vetrina... allora la devo incollare al centro di un'immagine bianca di 110x110 poichè lo script della vetrina richiede quello.

è li che mi cambia i colori... forse perchè utilizzo una thumb che è già stata creata con le GD???
o magari perchè non utilizzo imagecreatetruecolor ma solo imagecreate?? però se utilizzo imagecreatetruecolor non riesco ad ottenere lo sfondo bianco....
 
Ragazzi ho risolto!!!!!!!!!:quote:
PHP:
function createThumbnail_vetrina($img)
{ header("Content-type: image/jpg");
	$size = getimagesize("../thumb/".$img);
  $width = $size[0];
  $height = $size[1];
  if( $width < 110 ) $x = (110 - $width) / 2 ;
  if( $height < 110 ) $y = (110 - $height) / 2 ;
  // Resample the image.
  $im_sfondo = @imagecreate(110, 110) or die("Cannot Initialize new GD image stream");
  $im_sfondo = imagecolorallocate($im_sfondo, 255, 255, 255);
 
$dest = @imagecreatetruecolor(110, 110) or die("errore nel caricare gd");
$sfondo = imagecreatefromjpeg($im_sfondo);
$color= imagecolorallocate($dest, 255, 255, 255);
imagefilledrectangle($dest, 0, 0, 110, 110, $color);
$icona = imagecreatefromjpeg('../thumb/'.$img);
imagecopy($dest, $sfondo, 0, 0, 0, 0, 110, 110);
imagecopy($dest, $icona, $x, $y, 0, 0, $width, $height);
$newName="../vetrina/".$img;
imagejpeg($dest,$newName,100)or die("Cant save image");
$newName="vetrina/".$img;
//imagedestroy($sfondo);
return $newName;
}

Penso di aver capito che il problema era che non utilizzavo per $dest @imagecreatetruecolor ma solo imagecreate
Per risolvere lo sfondo nero ecco qua:
PHP:
$color= imagecolorallocate($dest, 255, 255, 255);
imagefilledrectangle($dest, 0, 0, 110, 110, $color);

Spero ti possa ancora essere utile alessandro1997:fonzie:
 

Discussioni simili