Note that there are some explanatory texts on larger screens.

plurals
  1. POError in creating thumbnails from my form
    primarykey
    data
    text
    <p>I've got a problem with a script of mine to create some thumbnails from images/photos that users can upload from my form in my personal website.</p> <p>First of all, after all the possible controls to prevent any possible attacks, I rename the photo uploaded from a user using some PHP functions, and then I'd like to show my logged user (and for the logging function I use here the sessions) his previously uploaded images/photos as thumbnails, giving then him the possibility to click on them and to obtain this way the photos with their original dimensions.</p> <p>The problem I got is with <strong><em>the thumbnails</em></strong>, that I'm not able to create!!!.</p> <p>I created a new table in my MySql database this way:</p> <pre><code>CREATE TABLE uploaded (id_file INT(11) UNSIGNED NOT NULL AUTO_INCREMENT url_file VARCHAR(50) NOT NULL, name_file VARCHAR(100) NOT NULL, type_file VARCHAR(20) NOT NULL, description VARCHAR(255), notes TEXT, data_insert DATETIME NOT NULL, address_IP CHAR(15) NOT NULL, username VARCHAR(20) NOT NULL, id_user INT(11) NOT NULL, PRIMARY KEY(id_file) ); </code></pre> <p>Then I created a file in PHP called <strong>vision.php</strong> to show the user his images, and this file parses itself another file in the query string this way:</p> <pre><code>echo '&lt;img src="thumb.php?id_file=' . $row['id_file'] . '" alt="' . $row['id_file'] . '" /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'; </code></pre> <p>The directory that contains the photos called <strong>“uploaded”</strong> is set with the permissions in 777. I gave in fact in a shell (I'm using a GNU/Linux distro) on my local server Apache this:</p> <p>sudo chmod 777 uploaded</p> <p>and I see there the photos I myself uploded on my localhost, where I always test the websites I create.</p> <p>Anyway online I got the same problem.</p> <p>Here is the source of the file I called <strong>vision.php</strong>:</p> <pre><code> &lt;?php try { // Report all PHP errors error_reporting(-1); $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo-&gt;prepare("SELECT * FROM uploaded WHERE id_user = ? ORDER BY id_file"); $stmt-&gt;execute(array($_SESSION['id_user'])); if ( $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC) ) { // Here I use a do/while looop do { echo '&lt;br /&gt;&lt;br /&gt;Your images:' . '&amp;nbsp; &amp;nbsp; &lt;a href="upload.php"&gt;Go back&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;'; echo '&lt;img src="thumb.php?id_file=' . $row['id_file'] . '" alt="' . $row['id_file'] . '" /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'; } while ( $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC) ); } else { echo '&lt;br /&gt;&lt;br /&gt; &lt;div class="centra"&gt;Hi, ' . $_SESSION['username'] . ', you sent me no images up to now. &lt;br /&gt;&lt;br /&gt; &lt;a href="upload.php"&gt;Send me an image/photo, if you wish&lt;/a&gt;&lt;/div&gt;'; include 'footer.php'; exit; } } // end try {} catch (PDOException $e) { echo "Error!: " . $e-&gt;getMessage() . "&lt;br /&gt;"; die; } ?&gt; </code></pre> <p>and this is the script I called <strong>thumb.php</strong>:</p> <pre><code>&lt;?php // I wanna have a jpeg image as thumbnail header('Content-type: image/jpeg'); // go on with the session session_start(); require_once 'config_db.php'; try { //chmod("./uploaded/", 0777); // Report all PHP errors error_reporting(-1); $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo-&gt;prepare("SELECT * FROM uploaded WHERE id_file = ? ORDER BY id_file"); // the parameter contained in the querystring in vision.php $stmt-&gt;execute(array($_GET['id_file'])); $row = stmt-&gt;fetch(PDO::FETCH_ASSOC); switch ($row['tipo_file']) { case 'png': $image = imagecreatefrompng('uploaded/' . $row['nome_file'] . '.' . $row['tipo_file']); break; case 'jpg' || 'jpeg': $image = imagecreatefromjpeg('uploaded/' . $row['nome_file'] . '.' . $row['tipo_file']); break; case 'gif': $image = imagecreatefromgif('uploaded/' . $row['nome_file'] . '.' . $row['tipo_file']); break; default: die('Impossible to get the image extension'); } $width = imagesx($image); $height = imagesy($image); $thumbsize = 250; $perc = min($thumbsize / $width, $thumbsize / $height); $thumb_width = intval($perc * $width); $thumb_height = intval($perc * $height); // The thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); imagejpeg($thumb, NULL, 80); // destroy the thumbnail to free some memory from the server imagedestroy($thumb); } // end try {} catch (PDOException $e) { echo "Error!: " . $e-&gt;getMessage() . "&lt;br /&gt;"; die; } ?&gt; </code></pre> <p>I'd like to add here some errors I can see from the error_file contained in the directory /opt/lampp/logs:</p> <p>I investigated this error_file contained in the directory /opt/lampp/logs and here I found this:</p> <pre><code>[Wed Jun 26 19:37:34.279824 2013] [:error] [pid 2933] [client 127.0.0.1:43215] PHP Warning:  **imagecreatefromjpeg(uploaded/046ccd6f930959786f71447f2ecf228d814481eb40481889d9ea619191a153cb12f585bbae867a66.jpeg): failed to open stream: No such file or directory in /opt/lampp/htdocs/new_site_local/thumb.php on line 35, referer:[http://localhost/new_site_local/vision.php]** [Wed Jun 26 19:37:34.279915 2013] [:error] [pid 2933] [client 127.0.0.1:43215] PHP Warning:  imagesx() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/new_site_local/thumb.php on line 47, referer:[ http://localhost/new_site_local/vision.php] [Wed Jun 26 19:37:34.279938 2013] [:error] [pid 2933] [client 127.0.0.1:43215] PHP Warning:  imagesy() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/new_site_local/thumb.php on line 48, referer: [http://localhost/new_site_local/vision.php] [Wed Jun 26 19:37:34.279961 2013] [:error] [pid 2933] [client 127.0.0.1:43215] PHP Warning:  Division by zero in /opt/lampp/htdocs/new_site_local/thumb.php on line 59, referer:[http://localhost/new_site_local/vision.php] </code></pre> <p>etc.</p> <p>Thanks all!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload