Konsul Δημοσ. 28 Νοεμβρίου 2011 Δημοσ. 28 Νοεμβρίου 2011 Γεια σας, θέλω να φτιάξω thumbnails από εικόνες on the fly στην PHP. Και βρήκα αυτή τη κλάση παρακάτω( ανάμεσα σε άλλες) > class thumb{ var $picture; var $index; function create(){ $picture = $this->picture; $index = $this->index; $test = pathinfo($picture); if (file_exists($picture)){ $test[extension] = strtolower($test['extension']); if ($test['extension'] == 'jpg' || $test['extension'] == 'jpeg'){ $do = TRUE; } else{ $do = FALSE; } } else{ $do = FALSE; } if ($do){ if (isset($index) && $index > 0 && $index < 101){ $index = str_replace(",", ".", $index); } else{ $index = 2; } $imageinfo = GetImageSize($picture); $thumbheight = $imageinfo[1]/$index; $thumbwidth = $imageinfo[0]/$index; $height = explode(".", $thumbheight); $width = explode(".", $thumbwidth); header("Content-type: image/jpeg"); $dest_img = ImageCreate($width[0], $height[0]); $src_img = ImageCreateFromJpeg($picture); ImageCopyResized($dest_img, $src_img, 0, 0, 0, 0, $width[0], $height[0], ImageSX($src_img), ImageSY($src_img)); ImageJpeg($dest_img); ImageDestroy($dest_img); ImageDestroy($src_img); } else{ exit(); } } } και το αρχείο που την καλεί ><?php require("class_thumb.php");//Here the class itself is included $thumb = new thumb;//The class is started //If you want to test this class, you need a picture called "bild.jpg" $thumb->picture = "images/disk.jpg";//Here you save the name of the picture /*The index must be a number between 0 and 101 If you choose for example $thumb->index = "2", the Thumbnail will be as half as greate as the original picture For savety is cared enough, because this class will work only with datas from the type image/jpeg or image/jpg and it checks if the index is a number and between 0 and 101. If the variable picture is no image the script will not work and end with exit(). If $index is not a number or not between 0 and 101 it will be set to 2. Everybody can use this class freely and give it around, but with a note, so that everybody can see who is tha author and his E-Mail adress (Daniel Thul, [email protected]) If you use this class, I would like if you write me an E-Mail, because I would like to see if anybody uses my class. Feel free to report me bugs or ideas for improvements. */ $thumb->index = "1.5";//Here the index is set $thumb->create();//This function creates the thumbnail and sends the picture with a header for the type "image/jpeg" to the browser ?> Το θέμα είναι ότι μου βγαίνει συνέχεια το παρακάτω error : The image http://localhost/img.php cannot be displayed because it contains errors. Φυσικά και το έψαξα στο google αλλά λύση δε βρήκα. Να πω ότι η GD Library στον local server μου είναι ενεργοποιημένη κανονικά. Αν ξέρετε για το error, αλλιώς να μου πείτε κάποια άλλη κλάση για thumbnails που δούλεψε σε εσάς. Ευχαριστώ εκ των προτέρων...
miza Δημοσ. 28 Νοεμβρίου 2011 Δημοσ. 28 Νοεμβρίου 2011 Τα παλιά τα χρόνια που έγραφα php, είχα χρησιμοποιήσει αυτό http://mightystuff.n...humbnail-script
Konsul Δημοσ. 28 Νοεμβρίου 2011 Μέλος Δημοσ. 28 Νοεμβρίου 2011 Τα παλιά τα χρόνια που έγραφα php, είχα χρησιμοποιήσει αυτό http://mightystuff.n...humbnail-script Υπονοείς κάτι για την PHP ? Το έκανα και αυτό και πάλι δεν μου δείχνει καθόλου τα images... επίσης δοκίμασα και το timthumb μαζί με κάποια άλλα και δεν μου εμφανίζονται πάλι τίποτα... Έχεις τίποτα άλλο για image resize on the fly οτιδήποτε ??
miza Δημοσ. 28 Νοεμβρίου 2011 Δημοσ. 28 Νοεμβρίου 2011 Υπονοείς κάτι για την PHP ? Όχι ρε, η PHP είναι τόσο καλή...ΝΟΤ Έχεις τίποτα άλλο για image resize on the fly οτιδήποτε ?? Είναι σίγουρα πρόβλημα με την βιβλιοθήκη, αν δεν βρίσκεις άκρη παίξε με άλλη βιβλιοθήκη, όπως Imagemagick, δες εδώ.
Crawl_From_Death Δημοσ. 29 Νοεμβρίου 2011 Δημοσ. 29 Νοεμβρίου 2011 Χρησιμοποιησε το εξης: αρχειο resize_image.php > <?php Class resize { // *** Class variables private $image; private $width; private $height; private $imageResized; function __construct($fileName) { // *** Open up the file $this->image = $this->openImage($fileName); // *** Get width and height $this->width = imagesx($this->image); $this->height = imagesy($this->image); } ## -------------------------------------------------------- private function openImage($file) { // *** Get extension $extension = strtolower(strrchr($file, '.')); switch($extension) { case '.jpg': case '.jpeg': $img = @imagecreatefromjpeg($file); break; case '.gif': $img = @imagecreatefromgif($file); break; case '.png': $img = @imagecreatefrompng($file); break; default: $img = false; break; } return $img; } ## -------------------------------------------------------- public function resizeImage($newWidth, $newHeight, $option="auto") { // *** Get optimal width and height - based on $option $optionArray = $this->getDimensions($newWidth, $newHeight, $option); $optimalWidth = $optionArray['optimalWidth']; $optimalHeight = $optionArray['optimalHeight']; // *** Resample - create image canvas of x, y size $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height); // *** if option is 'crop', then crop too if ($option == 'crop') { $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight); } } ## -------------------------------------------------------- private function getDimensions($newWidth, $newHeight, $option) { switch ($option) { case 'exact': $optimalWidth = $newWidth; $optimalHeight= $newHeight; break; case 'portrait': $optimalWidth = $this->getSizeByFixedHeight($newHeight); $optimalHeight= $newHeight; break; case 'landscape': $optimalWidth = $newWidth; $optimalHeight= $this->getSizeByFixedWidth($newWidth); break; case 'auto': $optionArray = $this->getSizeByAuto($newWidth, $newHeight); $optimalWidth = $optionArray['optimalWidth']; $optimalHeight = $optionArray['optimalHeight']; break; case 'crop': $optionArray = $this->getOptimalCrop($newWidth, $newHeight); $optimalWidth = $optionArray['optimalWidth']; $optimalHeight = $optionArray['optimalHeight']; break; } return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); } ## -------------------------------------------------------- private function getSizeByFixedHeight($newHeight) { $ratio = $this->width / $this->height; $newWidth = $newHeight * $ratio; return $newWidth; } private function getSizeByFixedWidth($newWidth) { $ratio = $this->height / $this->width; $newHeight = $newWidth * $ratio; return $newHeight; } private function getSizeByAuto($newWidth, $newHeight) { if ($this->height < $this->width) // *** Image to be resized is wider (landscape) { $optimalWidth = $newWidth; $optimalHeight= $this->getSizeByFixedWidth($newWidth); } elseif ($this->height > $this->width) // *** Image to be resized is taller (portrait) { $optimalWidth = $this->getSizeByFixedHeight($newHeight); $optimalHeight= $newHeight; } else // *** Image to be resizerd is a square { if ($newHeight < $newWidth) { $optimalWidth = $newWidth; $optimalHeight= $this->getSizeByFixedWidth($newWidth); } else if ($newHeight > $newWidth) { $optimalWidth = $this->getSizeByFixedHeight($newHeight); $optimalHeight= $newHeight; } else { // *** Sqaure being resized to a square $optimalWidth = $newWidth; $optimalHeight= $newHeight; } } return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); } ## -------------------------------------------------------- private function getOptimalCrop($newWidth, $newHeight) { $heightRatio = $this->height / $newHeight; $widthRatio = $this->width / $newWidth; if ($heightRatio < $widthRatio) { $optimalRatio = $heightRatio; } else { $optimalRatio = $widthRatio; } $optimalHeight = $this->height / $optimalRatio; $optimalWidth = $this->width / $optimalRatio; return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); } ## -------------------------------------------------------- private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight) { // *** Find center - this will be used for the crop $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); $crop = $this->imageResized; //imagedestroy($this->imageResized); // *** Now crop from center to exact requested size $this->imageResized = imagecreatetruecolor($newWidth , $newHeight); imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight); } ## -------------------------------------------------------- public function saveImage($savePath, $imageQuality="100") { // *** Get extension $extension = strrchr($savePath, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': if (imagetypes() & IMG_JPG) { imagejpeg($this->imageResized, $savePath, $imageQuality); } break; case '.gif': if (imagetypes() & IMG_GIF) { imagegif($this->imageResized, $savePath); } break; case '.png': // *** Scale quality from 0-100 to 0-9 $scaleQuality = round(($imageQuality/100) * 9); // *** Invert quality setting as 0 is best, not 9 $invertScaleQuality = 9 - $scaleQuality; if (imagetypes() & IMG_PNG) { imagepng($this->imageResized, $savePath, $invertScaleQuality); } break; // ... etc default: // *** No extension - No save. break; } imagedestroy($this->imageResized); } ## -------------------------------------------------------- } ?> σε οποιο αρχειο θελεις να κανεις κληση της κλασης χρησιμοποιεις την: > include("resize-class.php"); Για να κανεις resize μια εικονα χρησιμοποιεις τις εντολες: > $resizeObj = new resize(DIR_OF_IMAGE); $resizeObj -> resizeImage(100, 100, 'crop'); //διαστασεις της εικονας αφου γινει το thumb $resizeObj -> saveImage(NEW_DIR_OF_THE_THUMB, 100); Οι τελευταιες εντολες μπορουν να χρησιμοποιηθουν οσες φορες θελεις, αν για παραδειγμα θελεις να δημιουργησεις διαφορετικων διασταστεων thumbs για την ιδια εικονα.
vadoo Δημοσ. 30 Νοεμβρίου 2011 Δημοσ. 30 Νοεμβρίου 2011 Τσέκαρε μήπως έχει whitespace characters πρίν ή μετά τα php tags στο img.php και σε όσα κάνεις include. Το πρόβλημα μπορεί να προκαλείτα από κάποιο κενό, tab ή new line. Επίσης, για να το κάνεις debug: όπως βλέπεις το "This image cannot be displayed..." από τον browser κάνει File > Save σε ένα αρχείο και άνοιξέ το με ένα text editor. Μπορεί να έχει κάποιο PHP warning σε αρχή/τέλος.
Konsul Δημοσ. 2 Δεκεμβρίου 2011 Μέλος Δημοσ. 2 Δεκεμβρίου 2011 Eυχαριστώ για τις απαντήσεις με βοήθησαν και το έλυσα κάπως το πρόβλημα. Τώρα όμως έχω ένα άλλο και το λέω εδώ για να μην ανοίγω καινούργιο θέμα. Θέλω να κάνω upload αρχείου σε φόρμα και να κάνει RESIZE την φωτογραφία (σε διαστάσεις που θέλω εγώ) και μετά να την αποθηκεύει στην Βάση Δεδομένων MYSQL (χωρίς αρχεία και τέτοια, κατευθείαν στη βάση) ! Υπάρχει κάποιο script αγαπητοί insomniaks?
Uberalles_gr Δημοσ. 3 Δεκεμβρίου 2011 Δημοσ. 3 Δεκεμβρίου 2011 Καταρχάς ένα αρχείο δεν αποθηεκύεται στην βάση σου παρά μόνο στον server σου. Στην βάση κρατάμε το όνομα του αρχείου παράδειγμα για αυτό που θες πολύ γενικά > <form action="upload.php" method="post" name="upload_form" enctype="multipart/form-data"> <input type="file" name="upload_file" id="upload_file" /> </form> και στην upload.php γράφεις > <?php $upload_file = $_FILES["upload_file"]["name"]; //Ανέβασμα αρχείου μέσα στον φάκελο upload move_uploaded_file($_FILES["upload_file"]["tmp_name"],"upload/".$upload_file); //Αποθήκευση εικόνας στην βάση mysql_query("INSERT INTO upload (upload_file) VALUES ('".$upload_file."')"); ?>
defacer Δημοσ. 3 Δεκεμβρίου 2011 Δημοσ. 3 Δεκεμβρίου 2011 Καταρχάς ένα αρχείο δεν αποθηεκύεται στην βάση σου παρά μόνο στον server σου. Στην βάση κρατάμε το όνομα του αρχείου Φυσικά και μπορεί να το αποθηκεύσει στη βάση αν θέλει, σε BLOB (το αν αυτό είναι καλή ιδέα ή όχι και γιατί είναι αλλουνού παπά ευαγγέλιο).
Uberalles_gr Δημοσ. 3 Δεκεμβρίου 2011 Δημοσ. 3 Δεκεμβρίου 2011 Φυσικά και μπορεί να το αποθηκεύσει στη βάση αν θέλει, σε BLOB (το αν αυτό είναι καλή ιδέα ή όχι και γιατί είναι αλλουνού παπά ευαγγέλιο). Δεν το γνώριζα.. Μήπως defacer μπορείς να μας δείξεις πως γίνεται αυτό με κώδικα;
Crawl_From_Death Δημοσ. 3 Δεκεμβρίου 2011 Δημοσ. 3 Δεκεμβρίου 2011 Για το πως γινεται αυτο δειτε το παρακατω link http://forum.codecall.net/php-tutorials/6937-tutorial-storing-images-mysql-php.html
Konsul Δημοσ. 3 Δεκεμβρίου 2011 Μέλος Δημοσ. 3 Δεκεμβρίου 2011 κατάφερα να τις βάλω στη βάση σε BLOB, αλλά έχω πρόβλημα με το RESIZE δεν μπορω να βρω κάτι σχετικό... θέλω να τις κάνει RESIZE - SCALE - ή CROP πριν τις βάλει στη βάση. Αν γίνεται χωρίς να τις αποθηκεύσω σε αρχεία πριν τις βάλω... Γίνεται κάπως αυτό;
defacer Δημοσ. 3 Δεκεμβρίου 2011 Δημοσ. 3 Δεκεμβρίου 2011 κατάφερα να τις βάλω στη βάση σε BLOB, αλλά έχω πρόβλημα με το RESIZE δεν μπορω να βρω κάτι σχετικό... θέλω να τις κάνει RESIZE - SCALE - ή CROP πριν τις βάλει στη βάση. Αν γίνεται χωρίς να τις αποθηκεύσω σε αρχεία πριν τις βάλω... Γίνεται κάπως αυτό; Γίνεται, αλλά πριν σου πω το πώς να πω ότι καλό είναι να προσπαθείς να καταλάβεις τον κώδικα που χρησιμοποιείς, διαφορετικά ένα από τα άσχημα που θα συμβούν θα είναι να είσαι εξαρτημένος από άλλους και για τη μικρότερη των αλλαγών. Edit: Για να μην παρεξηγηθώ, δεν εννοώ ότι δεν προσπαθείς (δεν το ξέρω άλλωστε). Λοιπόν... αφού γίνει resize η εικόνα σου, αποθηκεύεται στο δίσκο (με τον κώδικα που έχεις τώρα) με την κλήση μιας function όπως η imagepng (ή imagejpeg κλπ, ανάλογα με το format που θέλεις). Αν διαβάσεις το documentation, θα δεις ότι η function παίρνει μια παράμετρο $filename για την οποία λέει: >filename The path to save the file to. If not set or NULL, the raw image stream will be outputted directly. Note: NULL is invalid if the quality and filters arguments are not used. Ωραία, οπότε αν δεν τη δώσεις καν τότε το raw image stream will be outputted directly (θα πάει στην έξοδο του script σου δηλαδή). Επομένως αυτό που χρειάζεται να κάνεις είναι να "τσιμπήσεις" αυτό το output και αντί να πάει στην έξοδο (που δεν το θέλεις), να το βάλεις σε μια μεταβλητή. Αυτό γίνεται με output buffering. Οπότε, εκεί που πριν είχες π.χ. >imagepng($image, 'resized.png'); // ή κάτι ανάλογο θα το αλλάξεις σε >ob_start(); imagepng($image); $bytes = ob_get_clean(); και η $bytes πλέον περιέχει αυτό που θέλεις να βάλεις μέσα στο BLOB.
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα