PHP Upload image and create link in file

Discussion in 'PHP' started by Wpisolutions, Sep 23, 2011.

  1. #1
    Hi all, i am currently trying to help a friend out with her website, She is a photographer and needs a portfolio page setting up for her.
    She is using multiple jquery lightbox plugin to display multiple groups of images on a single page. under different headings for example. Music, Weddings, Portraits.
    What i would like to do is this,
    1) Have a php file for each lightbox gallery included from the main page with the links for the images contained in them. weddings.php, music.php, portraits.php
    2) have an upload file, with a form that allows the user to enter data at time of upload so user can choose title of photo (title tag) and the folder in which to save. (save images in required folder - write correct link, to correct file, )
    3) have the data entered into the form create a link in the relevant php file.

    the structure of links needs to be
    <a href="img/weddings/image-1.jpg" class="lightbox" rel="weddings" title="some title">image #1</a>
    HTML:
    Am i making any sense?
     
    Wpisolutions, Sep 23, 2011 IP
  2. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #2
    What exactly is your question?
     
    jevchance, Sep 23, 2011 IP
  3. Wpisolutions

    Wpisolutions Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i have page upload.php
    with form to upload file (image) with
    <form enctype="multipart/form-data" action="process.php" method="post">Category: <select name="cat">
    <option value="weddings">Weddings</option>
    <option value="music">Music</option>
    <option value="portraits">Portraits</option></select><br />
    Name: <input name="flnm" type="text" />
    Descripton: <textarea name="desc" cols="30" rows="3"></textarea><br />
    Image:<input name="usrfile" type="file" /><br />
    <input name="submit" type="button" value="submit" />
    </form>
    HTML:
    i need to make that image be saved in folder depending on category and then thumbnail created and placed in folder with some prefix then add link to file called 'chosen category'.php following structure
    <a href="link-to-uploaded-image" class="lightbox" rel="chosen-catagory" title="chosen-descrption"><img src="link-to-chosen-image-thumb" width="100" height="40" alt="" /></a>
    HTML:
    does that make more sense? if easier maybe upload two images one being thumbnail?
    thank you.
     
    Last edited: Sep 23, 2011
    Wpisolutions, Sep 23, 2011 IP
  4. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #4
    I can tell you how I would handle this, maybe that will help you.

    Upload the image using an upload script. When the image is uploaded, you will use the gd library to create a resized version of the image, and a thumbnail. Here's a link for reference:

    http://www.php.net/manual/en/function.imagecopyresized.php

    I would create random filenames for the new images, and save them to a folder. All images can be saved in the same folder. I would then save those filenames to a database, and use that database to generate dynamic content on the various pages.

    I hope that gets you pointed in the right direction.
     
    jevchance, Sep 23, 2011 IP
  5. Wpisolutions

    Wpisolutions Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, so i still dont quite get this.

    Here is my form :
    <form enctype="multipart/form-data" action="upload.php" method="POST"> 
     Photo: <input type="file" name="photo"><br>
     Category: <select name="cat">
    <option value="weddings">Weddings</option>
    <option value="music">Music</option>
    <option value="portraits">Portraits</option></select>
    Title: <input type="text" name="title"><br> 
     <input type="submit" value="Upload photo"> 
     </form>
    Code (markup):
    form processed by upload.php :
    <?php 
     
     require "db.php";
     
     //Get form info 
     $name=$_POST['name']; 
     $title=$_POST['title']; 
     $category=$_POST['cat']; 
     $pic=($_FILES['photo']['name']); 
     //set target dir source file width height
     $target = $cat."/"; 
     $src = $target . basename( $_FILES['photo']['name']); 
     $destination = $cat."/thumbs";
     $thumbsrc = $destination . basename( $_FILES['photo']['name']); 
     $width = 200;
     $height = 200;
        header("content-type: image/jpeg");
        $src_img = imagecreatefromjpeg("$src");
        $srcsize = getimagesize("$dest");
        $dst_img = imagecreatetruecolor($width, $height);
    
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $srcsize[0], $srcsize[1]);
        imagejpeg($dst_img);
        
     //Unstert into db
     mysql_query("INSERT INTO `$category` VALUES ('$name', 'Stitle', '$src, $thumbsrc')") ; 
     
     //move photo 
     if(move_uploaded_file($_FILES['photo']['tmp_name'], $fulltarget)) 
     { 
     //Ok 
     echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
     } 
     else { 
     
     //not ok 
     echo "Sorry, there was a problem uploading your file."; 
     } 
     ?> 
    
    Code (markup):
    call a script from main page
    <div class="gallery">
     <?php include "weddings/get.php" ?>
     </div>
     <div class="gallery">
     <?php include "music/get.php" ?>
     </div>
    Code (markup):
    get.php displays links

    <?php
    include "db.php"; 
    $cat = weddings;
    $q = "SELECT id, title, src, thumbsrc FROM $cat";
    
    $result = $mysqli->query($q) or die(mysqli_error($mysqli));
    
    if ($result) {
        
        echo "<ul id='photos'> \n";
        
        while ($row = $result->fetch_object()) {
            
            $title = $row->title;
            $src = $row->src;
            $thsrc = $row->thumbsrc;
            $id = $row->id;
            
            echo "<a href='$src' class="lightbox" rel="$cat" title="$name - $title"><img src='$thsrc' id='$id' alt='$name' /></a>":
            }    
            
            echo "</ul>";
        
        
        
    }
    
    
    
    ?>
    Code (markup):
    I think i'm going along the right sort of path, but i'm not sure.
     
    Wpisolutions, Sep 23, 2011 IP
  6. Wpisolutions

    Wpisolutions Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So? can anyone help me with this?
     
    Wpisolutions, Sep 24, 2011 IP
  7. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #7
    You're definitely on the right track.

    In the line:
    $target = $cat."/";

    $cat is undefined.

    Get rid of:
    header("content-type: image/jpeg");

    Not sure why that is in there, we aren't delivering an image to the browser at this point.

    It also looks like there are some problems with your GD routine, but I don't have time to get into it until Monday. But here are some other pointers.

    Verify your server has GD installed, and what the capabilities are.

    
    <?php var_dump(gd_info()); ?>
    <?php echo phpinfo(); ?>
    
    Code (markup):
    Also, I'll provide you with a block of code I wrote for an administration tool for a product catalog. Take a look at the code, look through it line by line and try to figure out what each line is doing. I think this will help you conceptually.

    
    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/dynamic/';
    if (isset($_POST['PHP_UPLOAD']) && ($_POST['PHP_UPLOAD'] == "PHP_UPLOAD") && isset($_POST['MAX_FILE_SIZE'])) {
    	$ImageID = uniqid("");
    	$ThumbnailID = uniqid("");
    	$ProductID = $_POST['id'];
    	
    	$ext = strtolower(substr(basename($_FILES['theImage']['name']), strlen(basename($_FILES['theImage']['name']))-3));
    	if ($ext != 'jpg' && $ext != 'gif') {
    		header('Location: ' . $_SERVER['PHP_SELF'] . '?error=format&id=' . $_POST['id']);
    	}
    	$uploadfile = $uploaddir . $ImageID . '.jpg';
    	$thumbuploadfile = $uploaddir . $ThumbnailID . '.jpg';
    	if (move_uploaded_file($_FILES['theImage']['tmp_name'], $uploadfile)) {
    		//header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . basename($_FILES['theImage']['name']));
    
    		// Set Maximums
    		$width = 600;
    		$height = 450;
    		$thumbwidth = 100;
    		$thumbheight = 100;
    		
    		list($width_orig, $height_orig) = getimagesize($uploadfile);
    		
    		if ($width && ($width_orig < $height_orig)) {
    			$width = ($height / $height_orig) * $width_orig;
    		} else {
    			$height = ($width / $width_orig) * $height_orig;
    		}
    		
    		list($thumbwidth_orig, $thumbheight_orig) = getimagesize($uploadfile);
    		
    		if ($thumbwidth && ($thumbwidth_orig < $thumbheight_orig)) {
    			$thumbwidth = ($thumbheight / $thumbheight_orig) * $thumbwidth_orig;
    		} else {
    			$thumbheight = ($thumbwidth / $thumbwidth_orig) * $thumbheight_orig;
    		}
    		
    		if ($ext == 'jpg') {
    			// Resize and Resample
    			$image_p = imagecreatetruecolor($width, $height);
    			$image_t = imagecreatetruecolor($thumbwidth, $thumbheight);
    			$image = imagecreatefromjpeg($uploadfile);
    			imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    			imagecopyresampled($image_t, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $thumbwidth_orig, $thumbheight_orig);
    		
    			// Output
    			imagejpeg($image_p, $uploadfile, 100);
    			imagejpeg($image_t, $thumbuploadfile, 100);
    			
    			// Free memory
    			imagedestroy($image_p);
    			imagedestroy($image_t);
    			imagedestroy($image);
    		} elseif ($ext == 'gif') {
    			// Resize and Resample
    			$image_p = imagecreatetruecolor($width, $height);
    			$image_t = imagecreatetruecolor($thumbwidth, $thumbheight);
    			$image = imagecreatefromgif($uploadfile);
    			imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    			imagecopyresampled($image_t, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $thumbwidth_orig, $thumbheight_orig);
    		
    			// Output
    			imagejpeg($image_p, $uploadfile, 100);
    			imagejpeg($image_t, $thumbuploadfile, 100);
    			
    			// Free memory
    			imagedestroy($image_p);
    			imagedestroy($image_t);
    			imagedestroy($image);
    		}
    		header('Location: ' . $_SERVER['PHP_SELF'] . '?ImageID=' . $ImageID . '.jpg&ThumbnailID=' . $ThumbnailID . '.jpg&id=' . $ProductID);
    	} else {
    		echo "Error uploading file.<br />";
    		echo $uploadfile;
    		echo $_FILES['theImage']['error'];
    	}
    }
    
    Code (markup):
     
    jevchance, Sep 24, 2011 IP
  8. Wpisolutions

    Wpisolutions Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    here is a dump of GD
    
    array(12) {   ["GD Version"]=>   string(27) "bundled (2.0.34 compatible)"   ["FreeType Support"]=>   bool(true)   ["FreeType Linkage"]=>   string(13) "with freetype"   ["T1Lib Support"]=>   bool(true)   ["GIF Read Support"]=>   bool(true)   ["GIF Create Support"]=>   bool(true)   ["JPG Support"]=>   bool(true)   ["PNG Support"]=>   bool(true)   ["WBMP Support"]=>   bool(true)   ["XPM Support"]=>   bool(true)   ["XBM Support"]=>   bool(true)   ["JIS-mapped Japanese Font Support"]=>   bool(false) }   
    
    Code (markup):
    php is version 5.2.17

    i changed
     $target = $cat."/";
    Code (markup):
    to
    $target = $category."/";
    Code (markup):
    which appears to have help as did removing
    header("content-type: image/jpeg");
    Code (markup):
    also i moved the line with SQL query to end of file

     mysql_query("INSERT INTO `$category` VALUES ('$name', 'Stitle', '$src, $thumbsrc')") ; 
    
    Code (markup):
    the resulting errors

    Warning:  imagecreatefromjpeg(3.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 17
    
    Warning:  getimagesize(3.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 18
    
    Warning:  imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 21
    ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default  quality ��C           $.' ",#(7),01444'9=82<.342��C              2!!22222222222222222222222222222222222222222222222222����"��      ���}!1AQa"q2���#B��R��$3br�     %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������      ���w!1AQaq"2�B����    #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?���(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(���
    Warning:  move_uploaded_file() [function.move-uploaded-file]: Filename cannot be empty in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 26
    
    Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpx9WHx1' to '' in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 26
    Sorry, there was a problem uploading your file.
    Warning:  mysql_query() [function.mysql-query]: Access denied for user 'webroot'@'localhost' (using password: NO) in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 37
    
    Warning:  mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/vhosts/wpihosting.info/httpdocs/ex/vikyoungphoto/upload.php on line 37
    Code (markup):
     
    Wpisolutions, Sep 24, 2011 IP
  9. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Definitely a problem with the upload.

    I highly recommend moving that to a separate file, getting that to work first before adding in the image manipulation stuff.
     
    jevchance, Sep 24, 2011 IP