= 1) AND ($heightDiff >= 1) ) { $tn_width = $sourceWidth; $tn_height = $sourceHeight; } else { if (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 2 OR (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 0 AND $widthDiff > $heightDiff)) { $tn_width = ceil($heightDiff * $sourceWidth); $tn_height = $size; } else { $tn_height = ceil($widthDiff * $sourceHeight); $tn_width = $size; } } $tmp = imagecreatetruecolor($tn_width,$tn_height); /* Check if this image is PNG or GIF, then set if Transparent*/ if(($image_type == 1) OR ($image_type == 3)) { imagealphablending($tmp, false); imagesavealpha($tmp,true); $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127); imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent); } imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$sourceWidth,$sourceHeight); $flag = false; switch ($image_type) { case 1: $flag = imagegif($tmp, _PS_TMP_IMG_DIR_.$cacheImage); break; case 2: $flag = imagejpeg($tmp, _PS_TMP_IMG_DIR_.$cacheImage, 100); // best quality break; case 3: $flag = imagepng($tmp, _PS_TMP_IMG_DIR_.$cacheImage, 0); // no compression break; } imagedestroy($tmp); } return ''; } return ''; } /** * Check image upload * * @param array $file Upload $_FILE value * @param integer $maxFileSize Maximum upload size */ function checkImage($file, $maxFileSize) { if ($file['size'] > $maxFileSize) return Tools::displayError('image is too large').' ('.($file['size'] / 1000).Tools::displayError('KB').'). '.Tools::displayError('Maximum allowed:').' '.($maxFileSize / 1000).Tools::displayError('KB'); if (!isPicture($file)) return Tools::displayError('image format not recognized, allowed formats are: .gif, .jpg, .png'); if ($file['error']) return Tools::displayError('error while uploading image; change your server\'s settings'); return false; } function isPicture($file) { /* Detect mime content type */ $mime_type = false; $types = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png'); /*if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME); $mime_type = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); } else*/if (function_exists('mime_content_type')) $mime_type = mime_content_type($file['tmp_name']); elseif (function_exists('exec')) $mime_type = trim(exec('file -b --mime-type '.escapeshellarg($file['tmp_name']))); if (empty($mime_type) || $mime_type == 'regular file') $mime_type = $file['type']; if (($pos = strpos($mime_type, ';')) !== false) $mime_type = substr($mime_type, 0, $pos); // is it a picture ? return $mime_type && in_array($mime_type, $types); } /** * Check icon upload * * @param array $file Upload $_FILE value * @param integer $maxFileSize Maximum upload size */ function checkIco($file, $maxFileSize) { if ($file['size'] > $maxFileSize) return Tools::displayError('image is too large').' ('.($file['size'] / 1000).'ko). '.Tools::displayError('Maximum allowed:').' '.($maxFileSize / 1000).'ko'; if (substr($file['name'], -4) != '.ico') return Tools::displayError('image format not recognized, allowed formats are: .ico'); if ($file['error']) return Tools::displayError('error while uploading image; change your server\'s settings'); return false; } /** * Resize, cut and optimize image * * @param array $sourceFile Image object from $_FILE * @param string $destFile Destination filename * @param integer $destWidth Desired width (optional) * @param integer $destHeight Desired height (optional) * * @return boolean Operation result */ function imageResize($sourceFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = '') { list($sourceWidth, $sourceHeight, $image_type, $attr) = getimagesize($sourceFile); if (!$sourceWidth) return false; if ($destWidth == NULL) $destWidth = $sourceWidth; if ($destHeight == NULL) $destHeight = $sourceHeight; switch ($image_type) { case 1: $src = imagecreatefromgif($sourceFile); break; case 2: $src = imagecreatefromjpeg($sourceFile); break; case 3: $src = imagecreatefrompng($sourceFile); break; default: return ''; break; } $widthDiff = $destWidth / $sourceWidth; $heightDiff = $destHeight / $sourceHeight; if( ($widthDiff >= 1) AND ($heightDiff >= 1) ) { $tn_width = $sourceWidth; $tn_height = $sourceHeight; } else { if (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 2 OR (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 0 AND $widthDiff > $heightDiff)) { $tn_width = ceil($heightDiff * $sourceWidth); $tn_height = $destHeight; } else { $tn_height = ceil($widthDiff * $sourceHeight); $tn_width = $destWidth; } } $tmp = imagecreatetruecolor($tn_width,$tn_height); /* Check if this image is PNG or GIF, then set if Transparent*/ if(($image_type == 1) OR ($image_type == 3)) { imagealphablending($tmp, false); imagesavealpha($tmp,true); $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127); imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent); } imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$sourceWidth,$sourceHeight); $flag = false; switch ($image_type) { case 1: $flag = imagegif($tmp, $destFile); break; case 2: $flag = imagejpeg($tmp, $destFile, 100); // best quality break; case 3: $flag = imagepng($tmp, $destFile, 0); // no compression break; } imagedestroy($tmp); return $flag; } /** * Cut image * * @param array $srcFile Image object from $_FILE * @param string $destFile Destination filename * @param integer $destWidth Desired width (optional) * @param integer $destHeight Desired height (optional) * * @return boolean Operation result */ function imageCut($srcFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg', $destX = 0, $destY = 0) { if (!isset($srcFile['tmp_name']) OR !file_exists($srcFile['tmp_name'])) return false; // Source infos $srcInfos = getimagesize($srcFile['tmp_name']); $src['width'] = $srcInfos[0]; $src['height'] = $srcInfos[1]; $src['ressource'] = createSrcImage($srcInfos[2], $srcFile['tmp_name']); // Destination infos $dest['x'] = $destX; $dest['y'] = $destY; $dest['width'] = $destWidth != NULL ? $destWidth : $src['width']; $dest['height'] = $destHeight != NULL ? $destHeight : $src['height']; $dest['ressource'] = createDestImage($dest['width'], $dest['height']); $white = imagecolorallocate($dest['ressource'], 255, 255, 255); imagecopyresampled($dest['ressource'], $src['ressource'], 0, 0, $dest['x'], $dest['y'], $dest['width'], $dest['height'], $dest['width'], $dest['height']); imagecolortransparent($dest['ressource'], $white); $return = returnDestImage($fileType, $dest['ressource'], $destFile); return ($return); } function createSrcImage($type, $filename) { switch ($type) { case 1: return imagecreatefromgif($filename); break; case 3: return imagecreatefrompng($filename); break; case 2: default: return imagecreatefromjpeg($filename); break; } } function createDestImage($width, $height) { $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); return $image; } function returnDestImage($type, $ressource, $filename) { $flag = false; switch ($type) { case 'gif': $flag = imagegif($ressource, $filename); break; case 'png': $flag = imagepng($ressource, $filename, 7); break; case 'jpeg': default: $flag = imagejpeg($ressource, $filename, 90); break; } imagedestroy($ressource); return $flag; } /** * Delete product or category image * * @param integer $id_item Product or category id * @param integer $id_image Image id */ function deleteImage($id_item, $id_image = NULL) { $path = ($id_image) ? _PS_PROD_IMG_DIR_ : _PS_CAT_IMG_DIR_; $table = ($id_image) ? 'product' : 'category'; if (file_exists(_PS_TMP_IMG_DIR_.$table.'_'.$id_item.'.jpg')) unlink(_PS_TMP_IMG_DIR_.$table.'_'.$id_item.'.jpg'); if ($id_image AND file_exists($path.$id_item.'-'.$id_image.'.jpg')) unlink($path.$id_item.'-'.$id_image.'.jpg'); elseif (!$id_image AND file_exists($path.$id_item.'.jpg')) unlink($path.$id_item.'.jpg'); /* Auto-generated images */ $imagesTypes = ImageType::getImagesTypes(); foreach ($imagesTypes AS $k => $imagesType) if ($id_image AND file_exists($path.$id_item.'-'.$id_image.'-'.$imagesType['name'].'.jpg')) unlink($path.$id_item.'-'.$id_image.'-'.$imagesType['name'].'.jpg'); elseif (!$id_image AND file_exists($path.$id_item.'-'.$imagesType['name'].'.jpg')) unlink($path.$id_item.'-'.$imagesType['name'].'.jpg'); /* BO "mini" image */ if (file_exists(_PS_TMP_IMG_DIR_.$table.'_mini_'.$id_item.'.jpg')) unlink(_PS_TMP_IMG_DIR_.$table.'_mini_'.$id_item.'.jpg'); return true; } ?>