note 49909 added to function.imagecopyresized
| From: | fdalfornoATemailDOTit at osu1 dot php dot net | Date: | Fri, 11 Feb 2005 19:10:08 +0000 |
| Subject: | note 49909 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84854@lists.php.net to get a copy of this message | ||
This script help to resize an image
<?php
//controllo che la libreria sia installata
function checkGd(){
if (function_exists('ImageCopyResized')) {
return true;
}else{
return false;
}
}
function upload($campo,$directory,$pmime){
//recupero l'array con i dati del file uloadato
global $HTTP_POST_FILES;
//salvo il nome e il mime del file
$nomefile = $HTTP_POST_FILES[$campo]['name'];
$mime=$HTTP_POST_FILES[$campo]['type'];
if(! is_array($pmime)){
//il parametro mime non è un array
//lancio un errore a php
trigger_error ("mime non è un array: <br> <b>Ripassare capitolo V paragrafo II
su array :-P</b>", E_USER_ERROR);
return false;
}
if(! in_array($mime,$pmime)){
//controllo che il file sia di tipo corretto
//lancio un errore a php
trigger_error ("Il file caricato non è del tipo corretto: <br> <b>Ripassare
capitolo IV :-P</b>", E_USER_ERROR);
return false;
}
if (! move_uploaded_file($HTTP_POST_FILES[$campo]['tmp_name'], $directory.$nomefile)) {
switch ($HTTP_POST_FILES[$campo]['error']) {
case 0: break;
case 1: return "Il file inviato eccede le dimensioni specificate nel parametro
MAX_FILE_SIZE di php.ini";
break;
case 2: return "Il file inviato eccede le dimensioni specificate nel parametro
MAX_FILE_SIZE del form";break;
case 3: return "Upload eseguito parzialmente";
break;
case 4: return "Nessun file è stato inviato";
break;
default: return "Errore nella comunicazione";
break;
}
}
return true;
}
function controlSize($file,$maxHeight,$maxWidth){
list($width, $height, $type, $attr) = getimagesize($file);
if($height<=$maxHeight && $width <= $maxWidth){
return true;
}
return false;
}
function copyResize($file,$desFile,$misure,$bool){
//si esegue una riscalatura sulla altezza oppure sulla larghezza
list($width, $height, $mime, $attr) = getimagesize($file);
//controllo il mime del file
if($mime!=1 && $mime!=2 && $mime!=3){
trigger_error ("Scalatura di un file com mime non corretto ".$mime." : <br>
<b>Ripassare capitolo V paragrafo XLII funzioni per le immagini :-P</b>",
E_USER_ERROR);
return false;
}
if($bool == true){
$mwidth = (int)(($width * $misure)/$height);
$mheight = $misure;
}else{
$mheight = (int)(($height * $misure)/$width);
$mwidth = $misure;
}
//conforme al mime eseguo le funzioni corrette
switch($mime){
default:
case 2:
$src=ImagecreateFromJpeg($file);
$dest=ImageCreateTrueColor($mwidth,$mheight);
ImageCopyResized($dest,$src, 0,0,0,0,$mwidth,$mheight,$width,$height);
imagejpeg($dest,$desFile,80);
break;
case 3:
$src=ImagecreateFromPng($file);
$dest=ImageCreateTrueColor($mwidth,$mheight);
ImageCopyResized($dest,$src, 0,0,0,0,$mwidth,$mheight,$width,$height);
imagepng($dest,$desFile);
break;
case 1:
$src=ImagecreateFromGif($file);
$dest=ImageCreateTrueColor($mwidth,$mheight);
ImageCopyResized($dest,$src, 0,0,0,0,$mwidth,$mheight,$width,$height);
imagegif($dest,$desFile);
break;
}
return true;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49909
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49909&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49909&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49909&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49909&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49909&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49909&report=yes
Search -- http://master.php.net/manage/user-notes.php