SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using <img src> html tag inside php code

    hi
    How to echo $dest(it stores black image) using <img src> tag at the following php code (the complete funtion is not given),after the line of code,
    $dest = imagecreatetruecolor($w, $h);


    Code:
    function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
    {
        $tmpSrc     = pathinfo(strtolower($srcFile));
        $tmpDest    = pathinfo(strtolower($destFile));
        $size       = getimagesize($srcFile);
    
        if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
        {
           $destFile  = substr_replace($destFile, 'jpg', -3);
           $dest      = imagecreatetruecolor($w, $h);
           imageantialias($dest, TRUE);
        } elseif ($tmpDest['extension'] == "png") {
           $dest = imagecreatetruecolor($w, $h);
           imageantialias($dest, TRUE);
        } else {
          return false;
        }
    thanks,
    karthikanov24

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to save the image to a file first (use imagejpeg or imagepng with a filename) then echo the image tag pointing to the name you saved it as.

  3. #3
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    if i am adding this to a new file as u told above, say "newfile.php" which is as follows,

    Code:
    <?php
    $dest=imagecreatetruecolor($w,$h);
    imagejpeg($dest);
    ?>
    how can i get the values of argument $w,$h which is the argument of other funtion which is in another file..

    could u explain me with an example..please?

    thanks,
    karthika

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I didn't say add it to a new file, I said to save the image to a file.

    http://us2.php.net/function.imagejpeg

    The second argument of imagejpeg is the file name to write.

    Add it to your copy function

  5. #5
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    i tried this, which have html codes at line 14.
    echo "<img src=\"".$dest."\">"; is used here..

    Code:
    function createThumbnail($srcFile, $destFile, $width, $quality = 75)
    {
    	$thumbnail = '';
    		
    	if (file_exists($srcFile)  && isset($destFile))
    	{
    		$size        = getimagesize($srcFile);
    		$w           = number_format($width, 0, ',', '');
    		$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
    		
    		$thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
            
    
    }
            
    
    	// return the thumbnail file name on sucess or blank on fail
    	return basename($thumbnail);
    
    }	
    /*
    	Copy an image to a destination file. The destination
    	image size will be $w X $h pixels
    */
    function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
    {
        
        $tmpSrc     = pathinfo(strtolower($srcFile));
        $tmpDest    = pathinfo(strtolower($destFile));
        $size       = getimagesize($srcFile);
    
        if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
        {
           $destFile  = substr_replace($destFile, 'jpg', -3);
           $dest      = imagecreatetruecolor($w, $h);
           
          echo "<html>";
          echo "<body>";
          
          echo "<img src=\"".$dest."\">";
          echo "</body>";
          echo "</html>";
    
          imageantialias($dest, TRUE);
           
        } elseif ($tmpDest['extension'] == "png") {
           $dest = imagecreatetruecolor($w, $h);
           imageantialias($dest, TRUE);
        } else {
          return false;
        }
    
        switch($size[2])
        {
           case 1:       //GIF
               $src = imagecreatefromgif($srcFile);
                break;
           case 2:       //JPEG
                $src = imagecreatefromjpeg($srcFile);
                //imagejpeg($srcFile);  
               break;
           case 3:       //PNG
    when this function is called, i got 2 'no picture' symbol linearly arrage (i.e an ' x ' mark in red inside a box, instead of a black image which is output of imagecreatetruecolor().

    I right clicked on that symbol and saw the properties,it displays the path in the correct folder but its not a .jpg or.gif or .png file but a resource id like:
    Resource%20id%20#14 in the correct folder..

    Could u give me the reason why it shows such no picture symbol and why it does not show black image there..,please?

    thanks,
    karthika

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    $dest is an image object, not a file name, and you don't want to print the image tag before you create the image file.

    1) Create the image
    2) Save the image to a file
    3) Echo the image tag pointing to that file name

    Order matters

    But if this is the only output of your script, why not just show the image immediately instead of putting it in an <img> tag?

  7. #7
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to all! I understood..

    can u give me the url that covers the answer for my above all threads..
    Also if i am searching in google,what key words should i type in search bar
    to get the websites that cover all these..??

    thanks once again
    karthikanov24

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net

    The PHP manual covers everything needed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •