246 lines
10 KiB
PHP
246 lines
10 KiB
PHP
<?php
|
|
|
|
class Zend_Controller_Action_Helper_Image extends Zend_Controller_Action_Helper_Abstract
|
|
{
|
|
|
|
public function direct($PATH, $nomeFile,$ImageName)
|
|
{
|
|
############ Edit settings ##############
|
|
$ThumbSquareSize = 200; //Thumbnail will be 200x200
|
|
$BigImageMaxSize = 500; //Image Maximum height or width
|
|
$ThumbPrefix = "thumb_"; //Normal thumb Prefix
|
|
$DestinationDirectory = $PATH; //specify upload directory ends with / (slash)
|
|
$Quality = 90; //jpeg quality
|
|
##########################################
|
|
// check $_FILES['ImageFile'] not empty
|
|
if (!isset($_FILES[$nomeFile]) || !is_uploaded_file($_FILES[$nomeFile]['tmp_name'])) {
|
|
return FALSE; // output error when above checks fail.
|
|
}
|
|
// Random number will be added after image name
|
|
//$RandomNumber = rand(0, 9999999999);
|
|
//$ImageName = date('His').'_'.str_replace(' ', '-', strtolower($_FILES[$nomeFile]['name'])); //get image name
|
|
$ImageSize = $_FILES[$nomeFile]['size']; // get original image size
|
|
$TempSrc = $_FILES[$nomeFile]['tmp_name']; // Temp name of image file stored in PHP tmp folder
|
|
$ImageType = $_FILES[$nomeFile]['type']; //get file type, returns "image/png", image/jpeg, text/plain etc.
|
|
//$ImageType = 'image/png';
|
|
//Let's check allowed $ImageType, we use PHP SWITCH statement here
|
|
switch (strtolower($ImageType)) {
|
|
case 'image/png':
|
|
//Create a new image from file
|
|
$CreatedImage = imagecreatefrompng($_FILES[$nomeFile]['tmp_name']);
|
|
break;
|
|
case 'image/gif':
|
|
$CreatedImage = imagecreatefromgif($_FILES[$nomeFile]['tmp_name']);
|
|
break;
|
|
case 'image/jpeg':
|
|
case 'image/pjpeg':
|
|
$CreatedImage = imagecreatefromjpeg($_FILES[$nomeFile]['tmp_name']);
|
|
break;
|
|
default:
|
|
return FALSE; //output error and exit
|
|
}
|
|
//PHP getimagesize() function returns height/width from image file stored in PHP tmp folder.
|
|
//Get first two values from image, width and height.
|
|
//list assign svalues to $CurWidth,$CurHeight
|
|
list($CurWidth, $CurHeight) = getimagesize($TempSrc);
|
|
//set the Destination Image
|
|
$thumb_DestRandImageName = $DestinationDirectory . $ThumbPrefix . $ImageName; //Thumbnail name with destination directory
|
|
$DestFolder = $DestinationDirectory . $ImageName;
|
|
//Resize image to Specified Size by calling resizeImage function.
|
|
if ($this->saveImage($CurWidth, $CurHeight, $CreatedImage, $DestFolder, $Quality, $ImageType)) {
|
|
|
|
/*
|
|
We have succesfully resized and created thumbnail image
|
|
We can now output image to user's browser or store information in the database
|
|
echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">';
|
|
echo '<tr>';
|
|
echo '<td align="center"><img src="uploads/'.$ThumbPrefix.$NewImageName.'" alt="Thumbnail"></td>';
|
|
echo '</tr><tr>';
|
|
echo '<td align="center"><img src="uploads/'.$NewImageName.'" alt="Resized Image"></td>';
|
|
echo '</tr>';
|
|
echo '</table>';*/
|
|
|
|
} else {
|
|
return FALSE; //output error
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// This function will proportionally resize image
|
|
public function resizeImage($CurWidth, $CurHeight, $MaxSize, $DestFolder, $SrcImage, $Quality, $ImageType)
|
|
{
|
|
//Check Image size is not 0
|
|
if ($CurWidth <= 0 || $CurHeight <= 0) {
|
|
return FALSE;
|
|
}
|
|
//Construct a proportional size of new image
|
|
$ImageScale = min($MaxSize / $CurWidth, $MaxSize / $CurHeight);
|
|
$NewWidth = ceil($ImageScale * $CurWidth);
|
|
$NewHeight = ceil($ImageScale * $CurHeight);
|
|
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
|
|
// Resize Image
|
|
if (imagecopyresampled($NewCanves, $SrcImage, 0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight)) {
|
|
switch (strtolower($ImageType)) {
|
|
case 'image/png':
|
|
imagepng($NewCanves, $DestFolder);
|
|
break;
|
|
case 'image/gif':
|
|
imagegif($NewCanves, $DestFolder);
|
|
break;
|
|
case 'image/jpeg':
|
|
case 'image/pjpeg':
|
|
imagejpeg($NewCanves, $DestFolder, $Quality);
|
|
break;
|
|
default:
|
|
return FALSE;
|
|
}
|
|
//Destroy image, frees memory
|
|
if (is_resource($NewCanves)) {
|
|
imagedestroy($NewCanves);
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
}
|
|
|
|
//This function corps image to create exact square images, no matter what its original size!
|
|
public function cropImage($CurWidth, $CurHeight, $iSize, $DestFolder, $SrcImage, $Quality, $ImageType)
|
|
{
|
|
//Check Image size is not 0
|
|
if ($CurWidth <= 0 || $CurHeight <= 0) {
|
|
return FALSE;
|
|
}
|
|
//abeautifulsite.net has excellent article about "Cropping an Image to Make Square bit.ly/1gTwXW9
|
|
if ($CurWidth > $CurHeight) {
|
|
$y_offset = 0;
|
|
$x_offset = ($CurWidth - $CurHeight) / 2;
|
|
$square_size = $CurWidth - ($x_offset * 2);
|
|
} else {
|
|
$x_offset = 0;
|
|
$y_offset = ($CurHeight - $CurWidth) / 2;
|
|
$square_size = $CurHeight - ($y_offset * 2);
|
|
}
|
|
$NewCanves = imagecreatetruecolor($iSize, $iSize);
|
|
if (imagecopyresampled($NewCanves, $SrcImage, 0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size)) {
|
|
switch (strtolower($ImageType)) {
|
|
case 'image/png':
|
|
imagepng($NewCanves, $DestFolder);
|
|
break;
|
|
case 'image/gif':
|
|
imagegif($NewCanves, $DestFolder);
|
|
break;
|
|
case 'image/jpeg':
|
|
case 'image/pjpeg':
|
|
imagejpeg($NewCanves, $DestFolder, $Quality);
|
|
break;
|
|
default:
|
|
return FALSE;
|
|
}
|
|
//Destroy image, frees memory
|
|
if (is_resource($NewCanves)) {
|
|
imagedestroy($NewCanves);
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function saveImage($CurWidth, $CurHeight, $srcImage, $DestFolder, $Quality, $ImageType)
|
|
{
|
|
//Check Image size is not 0
|
|
if ($CurWidth <= 0 || $CurHeight <= 0) {
|
|
return FALSE;
|
|
}
|
|
|
|
$NewCanves = $srcImage;
|
|
|
|
switch (strtolower($ImageType)) {
|
|
case 'image/png':
|
|
imagesavealpha($NewCanves, true);
|
|
$color = imagecolorallocatealpha($NewCanves, 0, 0, 0, 127);
|
|
imagefill($NewCanves, 0, 0, $color);
|
|
imagepng($NewCanves, $DestFolder);
|
|
break;
|
|
case 'image/gif':
|
|
imagegif($NewCanves, $DestFolder);
|
|
break;
|
|
case 'image/jpeg':
|
|
case 'image/pjpeg':
|
|
imagejpeg($NewCanves, $DestFolder, $Quality);
|
|
break;
|
|
default:
|
|
return FALSE;
|
|
|
|
}
|
|
//Destroy image, frees memory
|
|
if (is_resource($NewCanves)) {
|
|
imagedestroy($NewCanves);
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
/**
|
|
* Funzione che passati i valori esegue l'operazione relativa in base all'analisi dei file attualmente presenti
|
|
*
|
|
* @param $inputFileName
|
|
* @param $formImageName
|
|
* @param $oldImageRecord
|
|
* @param $imagePath
|
|
* @param $nomeFile
|
|
* @param bool|true $randomizeImageName
|
|
*
|
|
* @return mixed|string
|
|
*/
|
|
public function image_operation($inputFileName, $formImageName, $oldImageRecord, $imagePath, $nomeFile, $randomizeImageName = true)
|
|
{
|
|
|
|
//Controllo che il nome del file sia valido
|
|
if (!empty($_FILES[$inputFileName]) && $_FILES[$inputFileName]['name'] != '') {
|
|
if($randomizeImageName){
|
|
$nomeFile = time() ."_". str_replace(' ', '-', strtolower($nomeFile));
|
|
}else{
|
|
$nomeFile = str_replace(' ', '-', strtolower($nomeFile));
|
|
}
|
|
}
|
|
|
|
//Vedo che operazione eseguire sul file immagine memorizzato nel file system
|
|
if (!empty($_FILES[$inputFileName]) && $nomeFile != '') {
|
|
if ($oldImageRecord != null) { //AGGIORNAMENTO
|
|
if ($oldImageRecord[$formImageName] != '' && file_exists($imagePath . $oldImageRecord[$formImageName])) {
|
|
unlink($imagePath . $oldImageRecord[$formImageName]); //Elimina la vecchia precendente dal file system
|
|
}
|
|
}
|
|
if (!$this->direct($imagePath, $inputFileName,$nomeFile)) { //Memorizza la nuova immagine
|
|
return 'error';
|
|
}
|
|
} elseif (empty($_FILES[$inputFileName]) && $nomeFile == '') { //CANCELLO SOLO L'IMMAGINE
|
|
if ($oldImageRecord != null) {
|
|
if ($oldImageRecord[$formImageName] != '' && file_exists($imagePath . $oldImageRecord[$formImageName])) {
|
|
unlink($imagePath . $oldImageRecord[$formImageName]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return (!$nomeFile) ? null : $nomeFile;
|
|
}
|
|
|
|
/**
|
|
* Controlla che l'immagine esiste e nel caso posistivo la elimina del fyle system
|
|
* @param $nomeFile
|
|
* @param $path
|
|
*/
|
|
public function deleteImage($nomeFile, $path)
|
|
{
|
|
if (file_exists($path . $nomeFile)) {
|
|
@unlink($path . $nomeFile);
|
|
}
|
|
}
|
|
|
|
}
|