djnijo
Mitglied
HI ich hab ein Problem und weis nicht woran das liegt. Hab ein Upload Basierend auf dem "Uploadify 3.2" script, und wollte diese so Modifizieren das es das Bild automatisch auf eine bestimmte Größe verkleinert und gleichzeitig ein Thumbnail erzeugt. Das speichern der datei durch "move_uploaded_file($tempFile,$picFile);" funktioniert, nur das resize nicht. Woran kann das liegen?
uploadify.php:
und die thumbnail.php:
uploadify.php:
PHP:
<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
// Define a destination
$picFolder = '../pics/pics'; // Relative to the root
$thumbFolder = '../pics/thumbnail';
$newName= time();
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
$width = "500";
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $picFolder;
$ext = explode('.',$_FILES['Filedata']['name']);
$extension = $ext[1];
$thumbFile = rtrim($thumbFolder,'/') . '/' . $newName.".".$extension;
$picFile = rtrim($picFolder,'/') . '/' . $newName.".".$extension;
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$picFile);
include_once("thumbnail.php");
$thumbnail = new thumbnail();
$thumbnail->create($picFile);
$thumbnail->setQuality(100);
$thumbnail->cube($rsize);
$thumbnail->save($thumbFile, true);
$picture = new thumbnail();
$picture->create($picFile);
$picture->setQuality(100);
$picture->resizeToWidth($width);
$picture->save($picFile, true);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
und die thumbnail.php:
PHP:
<?php
class thumbnail
{
var $img;
var $fileInfo;
var $fullName;
var $newX;
var $newY;
var $quality;
var $orgX;
var $orgY;
// $data - (voller) Dateiname oder String (z.B. aus Datenbank)
function create($data)
{
$this->destroy();
if (file_exists($data)) {
$this->img = @ImageCreateFromJpeg($data);
$this->fileInfo = basename($data);
$this->fullName = $data;
} else {
$this->img = @ImageCreateFromString($data);
}
if (!$this->img) {
$this->destroy();
return false;
} else {
$this->orgX = ImageSX($this->img);
$this->orgY = ImageSY($this->img);
return true;
}
}
// Höhe des aktuellen Bildes im Container zurückgeben, false bei Fehler
function height()
{
if ($this->img) {
return ImageSY($this->img);
} else {
return false;
}
}
// Breite des aktuellen Bildes im Container zurückgeben, false bei Fehler
function width()
{
if ($this->img) {
return ImageSX($this->img);
} else {
return false;
}
}
// Qualität für Ausgabe setzen
function setQuality($quality = false)
{
if ($this->img && $quality) {
$this->quality = $quality;
} else {
return false;
}
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
// Thumbnail erzeugen
function resize($newX = false,$newY = false)
{
if ($this->img) {
$X = ImageSX($this->img);
$Y = ImageSY($this->img);
$newX = $this->_convert($newX,$X);
$newY = $this->_convert($newY,$Y);
if (!$newX && !$newY) {
$newX = $X;
$newY = $Y;
}
if (!$newX) {
$newX = round($X / ($Y / $newY));
}
if (!$newY) {
$newY = round($Y / ( $X / $newX));
}
if ( ! $newimg = ImageCreateTruecolor($newX,$newY)) {
$newimg = ImageCreate($newX,$newY);
}
if ( ! ImageCopyResampled ($newimg, $this->img, 0, 0, 0, 0, $newX, $newY,$X,$Y)) {
ImageCopyResized ($newimg, $this->img, 0, 0, 0, 0, $newX, $newY,$X,$Y);
}
$this->img = $newimg;
return true;
} else {
return false;
}
}
// erzeugt ein Quadrat des Bildes mit Kantenlänge von $size
// ist das Bild nicht quadratisch kann mit $pos
// der Bildausschnitt festgelegt werden, Werte siehe function autocut
function cube($size,$pos = 5)
{
if ($this->img) {
$X = ImageSX($this->img);
$Y = ImageSY($this->img);
if ($X > $Y)
{
$newX = false;
$newY = $size;
} elseif ($X < $Y) {
$newX = $size;
$newY = false;
} else {
$newX = $size;
$newY = $size;
}
if ($this->resize($newX,$newY)) {
return $this->autocut($size,$size,$pos);
} else {
return false;
}
} else {
return false;
}
}
// speichert das Bild als $fileName
// wird $filename angegeben muss es ein voller Dateiname mit Pfad sein
// ist $override wahr wird ein bestehendes Bild überschrieben, sonst nicht
// Rückgabe:
// true wenn geschrieben (oder überschrieben)
// false on error
// 0 wenn schon existiert (nur bei $override=false)
function save($fileName, $override = true)
{
if ($this->img) {
if (!file_exists($fileName) || $override) {
if (ImageJPEG($this->img,$fileName,$this->quality)) {
return true;
} else {
return false;
}
} else {
return 0;
}
} else {
return false;
}
}
// Gibt Bild an Browser aus (Ausgabe des Headers, Destroy aufrufen), beide optional
function output($sendHeader = true,$destroy = true)
{
if ($this->img) {
if ($sendHeader) {
header("Content-type: image/jpeg");
}
ImageJPEG($this->img,"",$this->quality);
if ($destroy) {
$this->destroy();
}
} else {
return false;
}
}
// Setzt die Werte in der Klasse frei und löscht Bild
function destroy()
{
if ($this->img) {
ImageDestroy($this->img);
}
$this->img = false;
$this->fileInfo = false;
$this->fullName = false;
$this->newX = false;
$this->newY = false;
$this->quality = 70;
$this->orgX = false;
$this->orgY = false;
}
// rechnet prozentuale Angaben in Pixel um, erwartet
// ist $value eine Prozentangabe z.B. (string) "50%" wird diese umgerechnet
// $full muss als 100% in Pixel angegeben werden
function _convert($value,$full = false)
{
if (strstr($value,"%")) {
$value = trim(str_replace ("%", "", $value));
$value = ($full / 100) * $value;
}
if ($value < 1 && $value !== false) {
$value = 1;
}
return $value;
}
}
?>