Exemplo n.º 1
0
 /**
  * Saves the image to a file
  *
  * @param $filename string the name of the file to write to
  * @return bool|PEAR_Error TRUE or a PEAR_Error object on error
  * @access public
  */
 function save($filename, $type = '', $quality = null)
 {
     $options = is_array($quality) ? $quality : array();
     if (is_numeric($quality)) {
         $options['quality'] = $quality;
     }
     $quality = $this->_getOption('quality', $options, 75);
     imagick_setcompressionquality($this->imageHandle, $quality);
     if ($type && strcasecomp($type, $this->type) && !imagick_convert($this->imageHandle, $type)) {
         return $this->raiseError('Couldn\'t save image to file (conversion failed).', IMAGE_TRANSFORM_ERROR_FAILED);
     }
     if (!imagick_write($this->imageHandle, $filename)) {
         return $this->raiseError('Couldn\'t save image to file.', IMAGE_TRANSFORM_ERROR_IO);
     }
     $this->free();
     return true;
 }
Exemplo n.º 2
0
function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO', $force = false, $test_cache_only = false)
{
    // ordre de preference des formats graphiques pour creer les vignettes
    // le premier format disponible, selon la methode demandee, est utilise
    $image = $valeurs['fichier'];
    $format = $valeurs['format_source'];
    $destdir = dirname($valeurs['fichier_dest']);
    $destfile = basename($valeurs['fichier_dest'], "." . $valeurs["format_dest"]);
    $format_sortie = $valeurs['format_dest'];
    // liste des formats qu'on sait lire
    $img = isset($GLOBALS['meta']['formats_graphiques']) ? strpos($GLOBALS['meta']['formats_graphiques'], $format) !== false : false;
    // si le doc n'est pas une image, refuser
    if (!$force and !$img) {
        return;
    }
    $destination = "{$destdir}/{$destfile}";
    // chercher un cache
    $vignette = '';
    if ($test_cache_only and !$vignette) {
        return;
    }
    // utiliser le cache ?
    if (!$test_cache_only) {
        if ($force or !$vignette or @filemtime($vignette) < @filemtime($image)) {
            $creation = true;
            // calculer la taille
            if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) {
                if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) {
                    list($destWidth, $destHeight) = _image_ratio($valeurs['largeur'], $valeurs['hauteur'], $maxWidth, $maxHeight);
                }
            } elseif ($process == 'convert' or $process == 'imagick') {
                $destWidth = $maxWidth;
                $destHeight = $maxHeight;
            } else {
                spip_log("echec {$process} sur {$image}");
                return;
            }
            // Si l'image est de la taille demandee (ou plus petite), simplement
            // la retourner
            if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
                $vignette = $destination . '.' . $format;
                @copy($image, $vignette);
            } else {
                if ($process == 'convert') {
                    define('_CONVERT_COMMAND', 'convert');
                    define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -resize %xx%y! %src %dest');
                    $vignette = $destination . "." . $format_sortie;
                    $commande = str_replace(array('%x', '%y', '%src', '%dest'), array($destWidth, $destHeight, escapeshellcmd($image), escapeshellcmd($vignette)), _RESIZE_COMMAND);
                    spip_log($commande);
                    exec($commande);
                    if (!@file_exists($vignette)) {
                        spip_log("echec convert sur {$vignette}");
                        return;
                        // echec commande
                    }
                } else {
                    // imagick (php4-imagemagick)
                    if ($process == 'imagick') {
                        $vignette = "{$destination}." . $format_sortie;
                        $handle = imagick_readimage($image);
                        imagick_resize($handle, $destWidth, $destHeight, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
                        imagick_write($handle, $vignette);
                        if (!@file_exists($vignette)) {
                            spip_log("echec imagick sur {$vignette}");
                            return;
                        }
                    } else {
                        // netpbm
                        if ($process == "netpbm") {
                            define('_PNMSCALE_COMMAND', 'pnmscale');
                            // chemin a changer dans mes_options
                            if (_PNMSCALE_COMMAND == '') {
                                return;
                            }
                            $vignette = $destination . "." . $format_sortie;
                            $pnmtojpeg_command = str_replace("pnmscale", "pnmtojpeg", _PNMSCALE_COMMAND);
                            if ($format == "jpg") {
                                $jpegtopnm_command = str_replace("pnmscale", "jpegtopnm", _PNMSCALE_COMMAND);
                                exec("{$jpegtopnm_command} {$image} | " . _PNMSCALE_COMMAND . " -width {$destWidth} | {$pnmtojpeg_command} > {$vignette}");
                                if (!($s = @filesize($vignette))) {
                                    spip_unlink($vignette);
                                }
                                if (!@file_exists($vignette)) {
                                    spip_log("echec netpbm-jpg sur {$vignette}");
                                    return;
                                }
                            } else {
                                if ($format == "gif") {
                                    $giftopnm_command = str_replace("pnmscale", "giftopnm", _PNMSCALE_COMMAND);
                                    exec("{$giftopnm_command} {$image} | " . _PNMSCALE_COMMAND . " -width {$destWidth} | {$pnmtojpeg_command} > {$vignette}");
                                    if (!($s = @filesize($vignette))) {
                                        spip_unlink($vignette);
                                    }
                                    if (!@file_exists($vignette)) {
                                        spip_log("echec netpbm-gif sur {$vignette}");
                                        return;
                                    }
                                } else {
                                    if ($format == "png") {
                                        $pngtopnm_command = str_replace("pnmscale", "pngtopnm", _PNMSCALE_COMMAND);
                                        exec("{$pngtopnm_command} {$image} | " . _PNMSCALE_COMMAND . " -width {$destWidth} | {$pnmtojpeg_command} > {$vignette}");
                                        if (!($s = @filesize($vignette))) {
                                            spip_unlink($vignette);
                                        }
                                        if (!@file_exists($vignette)) {
                                            spip_log("echec netpbm-png sur {$vignette}");
                                            return;
                                        }
                                    }
                                }
                            }
                        } else {
                            if ($process == 'gd1' or $process == 'gd2') {
                                if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
                                    spip_log("vignette gd1/gd2 impossible : " . $srcWidth * $srcHeight . "pixels");
                                    return;
                                }
                                $destFormat = $format_sortie;
                                if (!$destFormat) {
                                    spip_log("pas de format pour {$image}");
                                    return;
                                }
                                $fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom'];
                                if (!function_exists($fonction_imagecreatefrom)) {
                                    return '';
                                }
                                $srcImage = @$fonction_imagecreatefrom($image);
                                if (!$srcImage) {
                                    spip_log("echec gd1/gd2");
                                    return;
                                }
                                // Initialisation de l'image destination
                                if ($process == 'gd2' and $destFormat != "gif") {
                                    $destImage = ImageCreateTrueColor($destWidth, $destHeight);
                                }
                                if (!$destImage) {
                                    $destImage = ImageCreate($destWidth, $destHeight);
                                }
                                // Recopie de l'image d'origine avec adaptation de la taille
                                $ok = false;
                                if ($process == 'gd2' and function_exists('ImageCopyResampled')) {
                                    if ($format == "gif") {
                                        // Si un GIF est transparent,
                                        // fabriquer un PNG transparent
                                        $transp = imagecolortransparent($srcImage);
                                        if ($transp > 0) {
                                            $destFormat = "png";
                                        }
                                    }
                                    if ($destFormat == "png") {
                                        // Conserver la transparence
                                        if (function_exists("imageAntiAlias")) {
                                            imageAntiAlias($destImage, true);
                                        }
                                        @imagealphablending($destImage, false);
                                        @imagesavealpha($destImage, true);
                                    }
                                    $ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
                                }
                                if (!$ok) {
                                    $ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
                                }
                                // Sauvegarde de l'image destination
                                $valeurs['fichier_dest'] = $vignette = "{$destination}.{$destFormat}";
                                $valeurs['format_dest'] = $format = $destFormat;
                                _image_gd_output($destImage, $valeurs);
                                if ($srcImage) {
                                    ImageDestroy($srcImage);
                                }
                                ImageDestroy($destImage);
                            }
                        }
                    }
                }
            }
        }
    }
    $size = @getimagesize($vignette);
    // Gaffe: en safe mode, pas d'acces a la vignette,
    // donc risque de balancer "width='0'", ce qui masque l'image sous MSIE
    if ($size[0] < 1) {
        $size[0] = $destWidth;
    }
    if ($size[1] < 1) {
        $size[1] = $destHeight;
    }
    $retour['width'] = $largeur = $size[0];
    $retour['height'] = $hauteur = $size[1];
    $retour['fichier'] = $vignette;
    $retour['format'] = $format;
    $retour['date'] = @filemtime($vignette);
    // renvoyer l'image
    return $retour;
}
Exemplo n.º 3
0
 /**
  * Save the image file
  *
  * @param $filename string the name of the file to write to
  *
  * @return none
  */
 function save($filename, $type = '', $quality = 75)
 {
     if (function_exists('imagick_setcompressionquality')) {
         imagick_setcompressionquality($this->imageHandle, $quality);
     }
     if ($type != '') {
         $type = strtoupper($type);
         imagick_write($this->imageHandle, $filename, $type);
     } else {
         imagick_write($this->imageHandle, $filename);
     }
     imagick_free($handle);
 }
Exemplo n.º 4
0
 /**
  * Save the image file
  *
  * @param $filename string the name of the file to write to
  *
  * @return none
  */
 function save($filename, $type = '', $quality = 75)
 {
     if ($type == '') {
         $type = strtoupper($type);
         imagick_write($this->imageHandle, $filename, $type);
     } else {
         imagick_write($this->imageHandle, $filename);
     }
     imagick_free($handle);
 }
Exemplo n.º 5
0
function action_tourner_post($r)
{
	$arg = $r[1];
	$row = sql_fetsel("fichier", "spip_documents", "id_document=$arg");

	if (!$row) return;

	include_spip('inc/charsets');	# pour le nom de fichier
	include_spip('inc/documents');
	// Fichier destination : on essaie toujours de repartir de l'original
	$var_rot = $r[2];
	include_spip('inc/distant'); # pour copie_locale
	$src = _DIR_RACINE . copie_locale(get_spip_doc($row['fichier']));
	if (preg_match(',^(.*)-r(90|180|270)\.([^.]+)$,', $src, $match)) {
		$effacer = $src;
		$src = $match[1].'.'.$match[3];
		$var_rot += intval($match[2]);
	}
	$var_rot = ((360 + $var_rot) % 360); // 0, 90, 180 ou 270

	if ($var_rot > 0) {
		$dest = preg_replace(',\.[^.]+$,', '-r'.$var_rot.'$0', $src);
		spip_log("rotation $var_rot $src : $dest");

		$process = $GLOBALS['meta']['image_process'];

		// imagick (php4-imagemagick)
		if ($process == 'imagick') {
			$handle = imagick_readimage($src);
			imagick_rotate($handle, $var_rot);
			imagick_write($handle, $dest);
			if (!@file_exists($dest)) return;	// echec imagick
		}
		else if ($process == "gd2") { // theoriquement compatible gd1, mais trop forte degradation d'image
			gdRotate ($src, $dest, $var_rot);
		}
		else if ($process == "convert") {
			if (_CONVERT_COMMAND!='') {
				define ('_CONVERT_COMMAND', 'convert');
				define ('_ROTATE_COMMAND', _CONVERT_COMMAND.' -rotate %t %src %dest');
			} else
				define ('_ROTATE_COMMAND', '');
			if (_ROTATE_COMMAND!=='') {
				$commande = str_replace(
					array('%t', '%src', '%dest'),
					array(
						$var_rot,
						escapeshellcmd($src),
						escapeshellcmd($dest)
					),
					_ROTATE_COMMAND);
				spip_log($commande);
				exec($commande);
			} else
				$dest = $src;
		}
	}
	else
		$dest = $src;

	$size_image = @getimagesize($dest);
	$largeur = $size_image[0];
	$hauteur = $size_image[1];

	// succes !
	if ($largeur>0 AND $hauteur>0) {
		sql_updateq('spip_documents', array('fichier' => set_spip_doc($dest), 'largeur'=>$largeur, 'hauteur'=>$hauteur), "id_document=$arg");
		if ($effacer) {
			spip_log("j'efface $effacer");
			spip_unlink($effacer);
		}
		// pipeline pour les plugins
		pipeline('post_edition',
			array(
				'args' => array(
					'table' => 'spip_documents',
					'table_objet' => 'documents',
					'spip_table_objet' => 'spip_documents',
					'type' =>'document',
					'id_objet' => $arg,
					'champs' => array('rotation'=>$r[2],'orientation'=>$var_rot,'fichier'=>$row),
					'serveur' => $serveur,
					'action'=>'tourner',
				),
				'data' => array('fichier'=>$row)
			)
		);
	}

}