function image_tag_changer_taille($tag, $width, $height, $style = false) { include_spip('inc/filtres_images_lib'); return _image_tag_changer_taille($tag, $width, $height, $style); }
/** * Recadre (rogne) une image en indiquant la taille de la découpe souhaitée * * On peut indiquer une proportion ou une taille spécifique, une position de rognage * et une couleur de fond, si le rognage est de taille plus grande que l'image d'origine. * * @example * - `[(#FICHIER|image_recadre{800, 400})]` * - `[(#FICHIER|image_recadre{800, 400, center})]` * - `[(#FICHIER|image_recadre{800, 400, center, black})]` * - `[(#FICHIER|image_recadre{16:9})]` * - `[(#FICHIER|image_recadre{16:9, -})]` (- est appliqué par défaut, équivalent à image_passe_partout) * - `[(#FICHIER|image_recadre{16:9, +, center, white})]` * - `[(#FICHIER|image_recadre{16:9, -, top left})]` * - `[(#FICHIER|image_recadre{16:9, -, top=40 left=20})]` * * @filtre * @uses _image_valeurs_trans() * @uses _image_tag_changer_taille() si image trop grande pour être traitée * @uses _image_ecrire_tag() * @link http://www.spip.net/5786 * * @param string $im * Chemin de l'image ou balise html `<img src=... />` * @param string|int $width * Largeur du recadrage * ou ratio sous la forme "16:9" * @param string|int $height * Hauteur du recadrage * ou "+" (agrandir) ou "-" (reduire) si un ratio est fourni pour width * @param string $position * Indication de position de la découpe : * - `center`, `left`, `right`, `top`, `bottom`, * - ou combinaisons de plusiers `top left` * - ou indication en pixels depuis une position `top=50` ou composée `top=40 left=50` * - ou nom d'une fonction spéciale qui calculera et retournera la position souhaitée * @param string $background_color * Couleur de fond si on agrandit l'image * @return string * balise image recadrée */ function image_recadre($im, $width, $height, $position = 'center', $background_color = 'white') { $fonction = array('image_recadre', func_get_args()); $image = _image_valeurs_trans($im, "recadre-{$width}-{$height}-{$position}-{$background_color}", false, $fonction); if (!$image) { return ""; } $x_i = $image["largeur"]; $y_i = $image["hauteur"]; if (_IMG_GD_MAX_PIXELS && $x_i * $y_i > _IMG_GD_MAX_PIXELS) { spip_log("image_recadre impossible sur {$im} : " . $srcWidth * $srcHeight . "pixels"); // on se rabat sur une reduction CSS return _image_tag_changer_taille($im, $width, $height); } // on recadre pour respecter un ratio ? // width : "16:9" // height : "+" pour agrandir l'image et "-" pour la croper if (strpos($width, ":") !== false) { list($wr, $hr) = explode(":", $width); $hm = $x_i / $wr * $hr; $ym = $y_i / $hr * $wr; if ($height == "+" ? $y_i < $hm : $y_i > $hm) { $width = $x_i; $height = $hm; } else { $width = $ym; $height = $y_i; } } if ($width == 0) { $width = $x_i; } if ($height == 0) { $height = $y_i; } $offset_width = $x_i - $width; $offset_height = $y_i - $height; $position = strtolower($position); // chercher une fonction spéciale de calcul des coordonnées de positionnement. // exemple 'focus' ou 'focus-center' avec le plugin 'Centre Image' if (!in_array($position, array('center', 'top', 'right', 'bottom', 'left'))) { if (count(explode(" ", $position)) == 1) { $positionner = charger_fonction("image_positionner_par_" . str_replace("-", "_", $position), "inc", true); if ($positionner) { $position = $positionner($im, $width, $height); } } } if (strpos($position, 'left') !== false) { if (preg_match(';left=(\\d{1}\\d+);', $position, $left)) { $offset_width = $left[1]; } else { $offset_width = 0; } } elseif (strpos($position, 'right') !== false) { $offset_width = $offset_width; } else { $offset_width = intval(ceil($offset_width / 2)); } if (strpos($position, 'top') !== false) { if (preg_match(';top=(\\d{1}\\d+);', $position, $top)) { $offset_height = $top[1]; } else { $offset_height = 0; } } elseif (strpos($position, 'bottom') !== false) { $offset_height = $offset_height; } else { $offset_height = intval(ceil($offset_height / 2)); } $im = $image["fichier"]; $dest = $image["fichier_dest"]; $creer = $image["creer"]; if ($creer) { $im = $image["fonction_imagecreatefrom"]($im); imagepalettetotruecolor($im); $im_ = imagecreatetruecolor($width, $height); @imagealphablending($im_, false); @imagesavealpha($im_, true); if ($background_color == 'transparent') { $color_t = imagecolorallocatealpha($im_, 255, 255, 255, 127); } else { $bg = _couleur_hex_to_dec($background_color); $color_t = imagecolorallocate($im_, $bg['red'], $bg['green'], $bg['blue']); } imagefill($im_, 0, 0, $color_t); imagecopy($im_, $im, max(0, -$offset_width), max(0, -$offset_height), max(0, $offset_width), max(0, $offset_height), min($width, $x_i), min($height, $y_i)); _image_gd_output($im_, $image); imagedestroy($im_); imagedestroy($im); } return _image_ecrire_tag($image, array('src' => $dest, 'width' => $width, 'height' => $height)); }
/** * Réduit une image * * @uses extraire_attribut() * @uses inserer_attribut() * @uses _image_valeurs_trans() * @uses _image_ratio() * @uses _image_tag_changer_taille() * @uses _image_ecrire_tag() * @uses _image_creer_vignette() * * @param array $fonction * Un tableau à 2 éléments : * 1) string : indique le nom du filtre de traitement demandé (par exemple : `image_reduire`) ; * 2) array : tableau reprenant la valeur de `$img` et chacun des arguments passés au filtre utilisé. * @param string $img * Chemin de l'image ou texte contenant une balise img * @param int $taille * Largeur désirée * @param int $taille_y * Hauteur désirée * @param bool $force * @param bool $cherche_image * Inutilisé * @param string $process * Librairie graphique à utiliser (gd1, gd2, netpbm, convert, imagick). * AUTO utilise la librairie sélectionnée dans la configuration. * @return string * Code HTML de la balise img produite **/ function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $cherche_image, $process = 'AUTO') { $image = false; if ($process == 'AUTO' and isset($GLOBALS['meta']['image_process'])) { $process = $GLOBALS['meta']['image_process']; } # determiner le format de sortie $format_sortie = false; // le choix par defaut sera bon if ($process == "netpbm") { $format_sortie = "jpg"; } elseif ($process == 'gd1' or $process == 'gd2') { $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction); // on verifie que l'extension choisie est bonne (en principe oui) $gd_formats = explode(',', $GLOBALS['meta']["gd_formats"]); if (is_array($image) and (!in_array($image['format_dest'], $gd_formats) or $image['format_dest'] == 'gif' and !function_exists('ImageGif'))) { if ($image['format_source'] == 'jpg') { $formats_sortie = array('jpg', 'png', 'gif'); } else { $formats_sortie = array('png', 'jpg', 'gif'); } // Choisir le format destination // - on sauve de preference en JPEG (meilleure compression) // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire # bug : gd_formats contient la liste des fichiers qu'on sait *lire*, # pas *ecrire* $format_sortie = ""; foreach ($formats_sortie as $fmt) { if (in_array($fmt, $gd_formats)) { if ($fmt != "gif" or function_exists('ImageGif')) { $format_sortie = $fmt; } break; } } $image = false; } } if (!is_array($image)) { $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction); } if (!is_array($image) or !$image['largeur'] or !$image['hauteur']) { spip_log("image_reduire_src:pas de version locale de {$img}"); // on peut resizer en mode html si on dispose des elements if ($srcw = extraire_attribut($img, 'width') and $srch = extraire_attribut($img, 'height')) { list($w, $h) = _image_ratio($srcw, $srch, $taille, $taille_y); return _image_tag_changer_taille($img, $w, $h); } // la on n'a pas d'infos sur l'image source... on refile le truc a css // sous la forme style='max-width: NNpx;' return inserer_attribut($img, 'style', "max-width: {$taille}px; max-height: {$taille_y}px"); } // si l'image est plus petite que la cible retourner une copie cachee de l'image if ($image['largeur'] <= $taille && $image['hauteur'] <= $taille_y) { if ($image['creer']) { @copy($image['fichier'], $image['fichier_dest']); } return _image_ecrire_tag($image, array('src' => $image['fichier_dest'])); } if ($image['creer'] == false && !$force) { return _image_ecrire_tag($image, array('src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest'])); } if (in_array($image["format_source"], array('jpg', 'gif', 'png'))) { $destWidth = $image['largeur_dest']; $destHeight = $image['hauteur_dest']; $logo = $image['fichier']; $date = $image["date_src"]; $preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force); if ($preview && $preview['fichier']) { $logo = $preview['fichier']; $destWidth = $preview['width']; $destHeight = $preview['height']; $date = $preview['date']; } // dans l'espace prive mettre un timestamp sur l'adresse // de l'image, de facon a tromper le cache du navigateur // quand on fait supprimer/reuploader un logo // (pas de filemtime si SAFE MODE) $date = test_espace_prive() ? '?' . $date : ''; return _image_ecrire_tag($image, array('src' => "{$logo}{$date}", 'width' => $destWidth, 'height' => $destHeight)); } else { return $img; } }
function image_recadre($im, $width, $height, $position = 'center', $background_color = 'white') { $fonction = array('image_recadre', func_get_args()); $image = _image_valeurs_trans($im, "recadre-{$width}-{$height}-{$position}-{$background_color}", false, $fonction); if (!$image) { return ""; } $x_i = $image["largeur"]; $y_i = $image["hauteur"]; if (_IMG_GD_MAX_PIXELS && $x_i * $y_i > _IMG_GD_MAX_PIXELS) { spip_log("image_recadre impossible sur {$im} : " . $srcWidth * $srcHeight . "pixels"); // on se rabat sur une reduction CSS return _image_tag_changer_taille($im, $width, $height); } if ($width == 0) { $width = $x_i; } if ($height == 0) { $height = $y_i; } $offset_width = $x_i - $width; $offset_height = $y_i - $height; $position = strtolower($position); if (strpos($position, 'left') !== FALSE) { if (preg_match(';left=(\\d{1}\\d+);', $position, $left)) { $offset_width = $left[1]; } else { $offset_width = 0; } } elseif (strpos($position, 'right') !== FALSE) { $offset_width = $offset_width; } else { $offset_width = intval(ceil($offset_width / 2)); } if (strpos($position, 'top') !== FALSE) { if (preg_match(';top=(\\d{1}\\d+);', $position, $top)) { $offset_height = $top[1]; } else { $offset_height = 0; } } elseif (strpos($position, 'bottom') !== FALSE) { $offset_height = $offset_height; } else { $offset_height = intval(ceil($offset_height / 2)); } $im = $image["fichier"]; $dest = $image["fichier_dest"]; $creer = $image["creer"]; if ($creer) { $im = $image["fonction_imagecreatefrom"]($im); imagepalettetotruecolor($im); $im_ = imagecreatetruecolor($width, $height); @imagealphablending($im_, false); @imagesavealpha($im_, true); if ($background_color == 'transparent') { $color_t = imagecolorallocatealpha($im_, 255, 255, 255, 127); } else { $bg = _couleur_hex_to_dec($background_color); $color_t = imagecolorallocate($im_, $bg['red'], $bg['green'], $bg['blue']); } imagefill($im_, 0, 0, $color_t); imagecopy($im_, $im, max(0, -$offset_width), max(0, -$offset_height), max(0, $offset_width), max(0, $offset_height), min($width, $x_i), min($height, $y_i)); _image_gd_output($im_, $image); imagedestroy($im_); imagedestroy($im); } return _image_ecrire_tag($image, array('src' => $dest, 'width' => $width, 'height' => $height)); }