Bonjour j'utilise actuellement ce script:
<?php
class ImgComponent extends Component {
function crop($source_image,$destination,$tn_w = 100,$tn_h = 100,$quality = 80,$wmsource = false) {
#find out what type of image this is
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/JPG':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
case 'image/PNG':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
$src_ratio = $src_w/$src_h;
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
if ($tn_w/$tn_h > $src_ratio) {
$new_h = $tn_w/$src_ratio;
$new_w = $tn_w;
} else {
$new_w = $tn_h*$src_ratio;
$new_h = $tn_h;
}
$x_mid = $new_w/2;
$y_mid = $new_h/2;
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($final, $newpic, 0, 0, ($x_mid-($tn_w/2)), ($y_mid-($tn_h/2)), $tn_w, $tn_h, $tn_w, $tn_h);
#if we need to add a watermark
if($wmsource) {
#find out what type of image the watermark is
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
die('Invalid watermark type.');
}
#if we're adding a watermark, figure out the size of the watermark
#and then place the watermark image on the bottom right of the image
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
}
imagejpeg($final,$destination,$quality);
return true;
}
}
Et mes png finisse croppé en png avec fond noir... alors que j'aimerai que le png soit en transparence...
Comment faire?
Merci d'avance.
Salut, est ce que tu passes par la fonction beforeSave pour cropper tes images ?
Si oui, est ce que pendant le cropping, ton image ne changerais pas d'extension.
Par ex, image initiale .png, image temporaire en .jpg pendant le cropping, image final en .png
Si c'est le cas tu perd la propriété de transparence par la conversion en jpg.
Non je ne passe pas par le beforeSave...
Mais par exemple, les image croppé, je ne peu pas les ouvrir avec photoshop, car il me dit que l'extension est inconnue... :s
J'utilise le crop ainsi, dans mon controlleur pages par exemple:
if($this->request->is('put') || $this->request->is('post')){
$file = $this->request->data"Release"]"artwork"];
//Creation du dossier artwork
$dir = 'img/artwork/';
if(!file_exists($dir))
mkdir($dir,0777);
//recuperation de l'extension
$f = explode('.',$file'name']);
$ext = '.'.end($f);
$nameofPic = $this->request->data"Release"]'matrice'];
$nameofPic = strtolower($nameofPic);
//Inflector slug permet de reformater le nom du fichier uploader pour ne pas avoir de nom avec caracteres speciaux inexploitable par le web
$filename = Inflector::slug($nameofPic);
move_uploaded_file($file'tmp_name'],$dir.$filename.$ext);
$success =$this->Release->save(array(
'matrice' => $this->request->data"Release"]'matrice'],
'studio' => $this->request->data"Release"]'studio'],
'vocals' => $this->request->data"Release"]'vocals'],
'musiciens' => $this->request->data"Release"]'musiciens'],
'inge' => $this->request->data"Release"]'inge'],
'mixed' => $this->request->data"Release"]'mixed'],
'format' => $this->request->data"Release"]'format'],
'creation' => $this->request->data"Release"]'creation'],
'artwork' => $filename.$ext,
'id' => $this->request->data"Release"]'id']
));
if($success){
sleep(8);
$source_image=$dir.DS.$filename.$ext;
$tn_w = 480;
$tn_h = 345;
$quality = 75;
$dest = $dir.'medium/'.$filename.$ext;
$this->Img->crop($source_image,$dest,$tn_w,$tn_h,$quality,false);
$tn_w = 110;
$tn_h = 110;
$quality = 75;
$dest = $dir.'thumb/'.$filename.$ext;
$this->Img->crop($source_image,$dest,$tn_w,$tn_h,$quality,false);
$this->Session->setFlash("Update completed","notif");
$this->redirect(array('action'=>"recordlabel"));
}
}
Hi,
Comme l'as dis Zenkiai, en utilisant la fonction imagejpeg() en fin de script, tu inhibe les propriétés des autres format à chaque fois...
@+