Questo è il codice:
Codice PHP:
public function download_product_images1($url, $filename, $idstore) {
if(image_exists($url)){
$max_width = 75;
$max_height = 75;
if(is_dir(ROOT_DIR.'/images/products/'.$idstore.'/')){
$destination = ROOT_DIR.'/images/products/'.$idstore.'/';
}else{
$destination = ROOT_DIR.'/images/products/'.$idstore.'/';
Mkdir(ROOT_DIR.'/images/products/'.$idstore.'/',0777);
}
$quality = '90';
$source_pic = ''.$url.'';
$ext = file_extension($url);
if(!file_exists(ROOT_DIR.'/images/products/'.$idstore.'/'.$filename.'.'.$ext)) {
if($ext == 'jpg'){
$src = imagecreatefromjpeg($source_pic);
}
if($ext == 'jpeg'){
$src = imagecreatefromjpeg($source_pic);
}
if($ext == 'gif'){
$src = imagecreatefromgif($source_pic);
}
if($ext == 'png'){
$src = imagecreatefrompng($source_pic);
}
list($width,$height)=getimagesize($source_pic);
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) ){
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$ext = file_extension($url);
$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
if($ext == 'jpg'){
$destination_pic = $destination.$filename.'.jpg';
imagejpeg($tmp,$destination_pic,$quality);
}
if($ext == 'gif'){
$destination_pic = $destination.$filename.'.gif';
imagegif($tmp,$destination_pic,$quality);
}
if($ext == 'png'){
$destination_pic = $destination.$filename.'.png';
imagepng($tmp,$destination_pic,$quality);
}
if($ext == 'jpeg'){
$destination_pic = $destination.$filename.'.jpg';
imagejpeg($tmp,$destination_pic,$quality);
}
imagedestroy($src);
imagedestroy($tmp);
return SITE_URL.'images/products/'.$idstore.'/'.$filename.'.'.$ext;
}else{
return SITE_URL.'images/products/'.$idstore.'/'.$filename.'.'.$ext;
}
}
}
Dove come parametri gli passo l'url dell'immagine, il nome dell'immagine l'id del negozio in questione.
Grazie