Ciao a tutti, ho trovato qui sul forum uno sript che effettua l'upload ed il resize in tre formati differenti.
Utilissimo per gestire ad esempio una galleria di foto.
Lo script, modificando i percorsi delle cartelle, funziona bene in locale (xampp), effettua l'upload ed il resize.
Caricato il tutto su server linux aruba sembra funzionare, da anche il messaggio di avvenuto caricamento, ma non effettua l'upload nelle apposite cartelle.
Ho settato i permessi della cartella delle immagini come 777, i permessi della cartella che contiene lo scipt sono 755...dove sbaglio?
Ecco lo script:
Qui controllo se hai effettuato il login:
[php]
// Includo la connessione al database
require('connessione.php');
// Se non è stata definita la variabile manda l'utente alla homepage
if(!isset($_SESSION['login']))
{
header('Location: index.php');
exit;
}
[/php]
Script:
[php]
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
// file needs to be jpg,gif,bmp,x-png and 4 MB max
if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"] < 4000000))
{
//PERCORSO FILE E MINIATURE SUL SERVER LOCALE
//$remote_file_full = "../sito/cartellafoto/foto/".$_FILES["image_upload_box"]["name"];
//$remote_file_240 = "..sito/cartellafoto/foto/thumb/".$_FILES["image_upload_box"]["name"];
//$remote_file_120 = "../sito/cartellafoto/foto/mini/".$_FILES["image_upload_box"]["name"];
//PERCORSO FILE E MINIATURE SUL SERVER WEB
$remote_file_full = "../cartellafoto/foto/".$_FILES["image_upload_box"]["name"];
$remote_file_240 = "../cartellafoto/foto/thumb/".$_FILES["image_upload_box"]["name"];
$remote_file_120 = "../cartellafoto/foto/mini/".$_FILES["image_upload_box"]["name"];
//chmod($remote_file_full,0644);
//chmod($remote_file_240,0644);
//chmod($remote_file_120,0644);
// carico la foto originale
move_uploaded_file($_FILES['image_upload_box']['tmp_name'], $remote_file_full);
copy($remote_file_full,$remote_file_240);
copy($remote_file_full,$remote_file_120);
// procedo a ridimensionare la foto
include("class.image-resize.php");
$obj = new img_opt();
$obj->max_width(287);
$obj->max_height(294);
$obj->image_path($remote_file_240);
$obj->image_resize();
$obj2 = new img_opt();
$obj2->max_width(88);
$obj2->max_height(92);
$obj2->image_path($remote_file_120);
$obj2->image_resize();
header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
exit;
}
else{
header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
exit;
}
}
[/php]
Ecco la classe da includere:
[php]
class img_opt
{
var $max_width;
var $max_height;
var $path;
var $img;
var $new_width;
var $new_height;
var $mime;
var $image;
var $width;
var $height;
function max_width($width)
{
$this->max_width = $width;
}
function max_height($height)
{
$this->max_height = $height;
}
function image_path($path)
{
$this->path = $path;
}
function get_mime()
{
$img_data = getimagesize($this->path);
$this->mime = $img_data['mime'];
}
function create_image()
{
switch($this->mime)
{
case 'image/jpeg':
$this->image = imagecreatefromjpeg($this->path);
break;
case 'image/gif':
$this->image = imagecreatefromgif($this->path);
break;
case 'image/png':
$this->image = imagecreatefrompng($this->path);
break;
}
}
function image_resize()
{
set_time_limit(120);
$this->get_mime();
$this->create_image();
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
$this->set_dimension();
$image_resized = imagecreatetruecolor($this->new_width,$this->new_height);
imagecopyresampled($image_resized, $this->image, 0, 0, 0, 0, $this->new_width, $this->new_height,$this->width, $this->height);
imagejpeg($image_resized,$this->path);
}
//######### FUNCTION FOR RESETTING DEMENSIONS OF IMAGE ###########
function set_dimension()
{
if($this->width==$this->height)
{
$case = 'first';
}
elseif($this->width > $this->height)
{
$case = 'second';
}
else
{
$case = 'third';
}
if($this->width>$this->max_width && $this->height>$this->max_height)
{
$cond = 'first';
}
elseif($this->width>$this->max_width && $this->height<=$this->max_height)
{
$cond = 'first';
}
else
{
$cond = 'third';
}
switch($case)
{
case 'first':
$this->new_width = $this->max_width;
$this->new_height = $this->max_height;
break;
case 'second':
$ratio = $this->width/$this->height;
$amount = $this->width - $this->max_width;
$this->new_width = $this->width - $amount;
$this->new_height = $this->height - ($amount/$ratio);
break;
case 'third':
$ratio = $this->height/$this->width;
$amount = $this->height - $this->max_height;
$this->new_height = $this->height - $amount;
$this->new_width = $this->width - ($amount/$ratio);
break;
}
}
}
[/php]
Sul server aruba ho provato tutti i tipi di percorsi, assoluto, con www, ma non capisco dove è l'errore.
Ho anche provato a mettere il tutto in un'altra cartella protetta da password creata attraverso il pannello di controllo aruba.
Ripeto che lo script, cambiando i percorsi delle cartelle, in locale funziona correttamente, se riuscite a farlo funzionare su aruba/linux e mi date una mano sarei felicissimo!
Ciaoo