Jump to content

From .tiff to jpeg


GleyRejeb

Recommended Posts

Hello Everybody,

How can i convert an image from .tiff to .jpg using php ?

please , need help !!!

I've already seen many exples on Stackoverflow but there were not helpful.

I'am building a bildgallery using PHP and i created a thumbnails to save the images in this thunmbnails . but i didnt know that many browsers like Chrome or firefox dont support the TIFF files .so i dont hv a choice just to create a PHP function to convert my TIFF files to jpg or png ...

Do you have any Idea ?

Hier is my Code :

<?php
$my_images_arr = scandir("images");
// print_r($my_images_arr);
$img_string = "";
//  $imageFileTyp = pathinfo('images', PATHINFO_EXTENSION);



foreach ($my_images_arr as $image_name) {
	if(
			strlen ($image_name) > 2   && strrpos($image_name,'.db') != true   
			
			)
	
		
      			
	{
		$img_string .= '<img src ="images/'.$image_name.'">';
		
 
	}
	
	
	elseif 
	(
		strrpos($image_name,'.tif') === true
	)
	
	{	
	$image_name = preg_replace('.tif', '.jpg', $image_name);
	
	}
	

	
}

?>



   


<!DOCTYPE html>
<html >
<head>
    <title> Bilder Galerie </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
   
  <style type="text/css">

     
      div#buttons{
      position:absolute;
      margin-left:-258px;
      
      }
       div#gHolder{
            width: 1348px;
            height: auto;
            margin: 0 auto;
            background-color: #FFA500;
            padding: 10px;
            position:fixed;
       }
       div#theBigImageHolder{
            width: 0px;
            height: 0px;
            background-color: white;
          
            
       }
       div#thumbnailsHolder{
            width: 393px;
            height: 940px;
            background-color: white;
            margin: -8px 14px auto;
            padding: 4px;
            overflow: auto;
            float: none;
       }
	       div#Holder{
	         background-color: #FFA500;
	         width: 530px;
	         height: 960px;
	         float: right;
        
       }

       div#theBigImageHolder > img{
           
            border: 15px solid #3e2b14;
            width: 900px;
	         height: 917px;
           /*margin: 71px 447px auto;*/         
           /*box-shadow: 0px 0px 3px 749px rgba(11, 12, 12, 0.95);*/
             
		     margin-top: -8px;
		     margin-left:422px;
		     top: 0;
		     left: 0;
		     right: 0;
		     bottom: 0;*/
		     
		    
	}
		       
       div#thumbnailsHolder > img{
           width: 90px;
           height: 90px;
           display: block;
           float: left;
           margin: 2px;
           transition: border-radius 0.3s linear 0s;
           overflow: auto;
       }
       div#thumbnailsHolder > img:hover{
           border-radius: 100px;
           cursor:pointer;

       }
       
           
	  </style>

	  <script type ="text/javascript">
	      function imgFunc() {
	          var bigImage = document.getElementById("BigImage");
	          var thumbnailsHolder = document.getElementById("thumbnailsHolder");

         thumbnailsHolder.addEventListener("click",function(event){
             if (event.target.tagName == "IMG"){
                 bigImage.src = event.target.src;
             }
            
         },false);
      }
      window.addEventListener("load",imgFunc,false);


      


   
      

   </script>
</head>

<body>
 


     <div id ="gHolder">
       <div id="theBigImageHolder">
          <?php
              echo '<img src="images/'.$my_images_arr[0].'" id="BigImage">';
           ?> 
           <!--  <img src="images/mench.jpg" id="BigImage"> -->
       </div>
       <div id="thumbnailsHolder">
             
              <?php
                 echo $img_string;     
               ?>

              <!-- <img src="images/name1.jpg">
               <img src="images/wolf.jpg">
               <img src="images/panda.jpg">
               <img src="images/hund.jpg">  
               -->
               
               
         
       </div>
     </div>  

     <div id="Holder">
<!--        <div id="buttons"> -->
<!--        <form  action="upload.php" method="post" enctype="multipart/form-data"> -->
<!--        <p>Bitte eine Datei auswaehlen<br> -->
<!--        <input type="file" name="image"> <br> -->
<!--        <input type="submit"  value="Hochladen">   -->
       
       
       
<!--        </form> -->
<!--        </div> -->
     
    <center> 
    <br><br> <form action="upload.php" method="post" enctype="multipart/form-data">
		Datei auswaehlen:<input type="file" name="image"><br /><br />
		beschreibung:<input type="text" name="desc"><br /><br />
		<input type="submit" name="upload" value="Hochladen">
	</form>
	</center>
	     </div>  

</body>
</html>

 

Link to comment
Share on other sites

No i can't, because i want to build this for clients and i work with peaple who use many files from TIFF  , thats why the gd way is to create a php function ...

Link to comment
Share on other sites

The PHP GD library can't open TIFF files, but it looks like ImageMagick can. If you don't have access to ImageMagick then you'll have to go through the tedious work of building a TIFF reader that parses binary TIFF files and extracts information from them.

  • Like 1
Link to comment
Share on other sites

Hi again ,

i added this code to my script , but i cant move on ..

can someone help me ??

<?php
$imageOrigine = "images/cat.tiff";
 
$image = new Imagick();           
$image->pingImage($imageOrigine); 
$image->readImage($imageOrigine); 
$image->setImageFormat("jpg");    
$image->writeImage("temp.jpg");

?>

 

Link to comment
Share on other sites

On 19.6.2017 at 6:33 PM, justsomeguy said:

What happens when you run that?

thank you 4 ur answer ..

Nothing happened, I wanted to make this in 3 step just when i clik on upload button :

1-convert
2-save this in images folder
3-display on the browser


<?php
$my_images_arr = scandir("images");
$img_string = "";
$image= 'C:/Program Files/Lexmark/Solutions/Apache2/htdocs/test/images';

 
//   $image = new Imagick('cat.tiff');
//   $image->setimageformat('png');
//   echo $image;
 



foreach ($my_images_arr as $image_name) {
    if(
            strlen ($image_name) > 2   &&  strrpos($image_name,'.db') != true   
            
            )
    
        
                  
    {
        $img_string .= '<img src ="images/'.$image_name.'">';
        
 
    }
     
    
  
}

?>








   


<!DOCTYPE html>
<html >
<head>
    <title> Bilder Galerie </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
   
  <style type="text/css">

     
      div#buttons{
      position:absolute;
      margin-left:-258px;
      
      }
       div#gHolder{
            width: 1348px;
            height: auto;
            margin: 0 auto;
            background-color: #FFA500;
            padding: 10px;
            position:fixed;
       }
       div#theBigImageHolder{
            width: 0px;
            height: 0px;
            background-color: white;
          
            
       }
       div#thumbnailsHolder{
            width: 393px;
            height: 940px;
            background-color: white;
            margin: -8px 14px auto;
            padding: 4px;
            overflow: auto;
            float: none;
       }
           div#Holder{
             background-color: #FFA500;
             width: 530px;
             height: 960px;
             float: right;
        
       }

       div#theBigImageHolder > img{
           
           border: 15px solid #3e2b14;
           width: 900px;
           height: 917px;
           /*margin: 71px 447px auto;*/         
           /*box-shadow: 0px 0px 3px 749px rgba(11, 12, 12, 0.95);*/
             
           margin-top: -8px;
           margin-left:422px;
           top: 0;
           left: 0;
           right: 0;
           bottom: 0;*/
             
            
    }
               
       div#thumbnailsHolder > img{
           width: 90px;
           height: 90px;
           display: block;
           float: left;
           margin: 2px;
           transition: border-radius 0.3s linear 0s;
           overflow: auto;
       }
       div#thumbnailsHolder > img:hover{
           border-radius: 100px;
           cursor:pointer;

       }
       
           
      </style>

      <script type ="text/javascript">
          function imgFunc() {
              var bigImage = document.getElementById("BigImage");
              var thumbnailsHolder = document.getElementById("thumbnailsHolder");

         thumbnailsHolder.addEventListener("click",function(event){
             if (event.target.tagName == "IMG"){
                 bigImage.src = event.target.src;
             }
            
         },false);
      }
      window.addEventListener("load",imgFunc,false);


      


   
      

   </script>
</head>

<body>
 


     <div id ="gHolder">
       <div id="theBigImageHolder">
          <?php
              echo '<img src="images/'.$my_images_arr[0].'" id="BigImage">';
           ?> 
       
     
       </div>
       <div id="thumbnailsHolder">
             
           
              
              <?php
                 echo $img_string;  
               ?>
 
              
               
               
         
       </div>
     </div>  

     <div id="Holder">
<!--        <div id="buttons"> -->
<!--        <form  action="upload.php" method="post" enctype="multipart/form-data"> -->
<!--        <p>Bitte eine Datei auswaehlen<br> -->
<!--        <input type="file" name="image"> <br> -->
<!--        <input type="submit"  value="Hochladen">   -->
       
       
       
<!--        </form> -->
<!--        </div> -->
     
    <center> 
    <br><br> <form action="index.php" method="post" enctype="multipart/form-data">
        Datei auswaehlen:<input type="file" name="image"><br /><br />
        beschreibung:<input type="text" name="desc"><br /><br />
        <input type="submit" name="upload" value="Hochladen">
    </form>
    <hr style ="border:2px solid green;">
    </center>
    <center>
    
    <?php
    
       $im= new Imagick();
    
    if(isset($_POST['upload']))
    {
    	
    	
    	$image_name = $_FILES['image']['name'];
	    $image_type = $_FILES['image']['type'];
	    $image_size = $_FILES['image']['size'];
	    $image_tmp_name= $_FILES['image']['tmp_name'];
	    $desc = $_POST['desc'];
	    $im->setimageformat('png');
// 	    header( "Content-Type: image/png" );
// 	    echo $im;
	   
	    
	    
           

    move_uploaded_file($image_tmp_name,"images/$image_name");

        echo "<img src='images/$image_name' width='400' height='250'><br>$desc";
       
        
           
    }
    ?>
    </center>
         </div>  

</body>
</html>

 

Edited by GleyRejeb
Link to comment
Share on other sites

Something has to happen when you run that code in the earlier post.  Either it's going to create a new image, or there's going to be an error.  If it's not creating a new image, then make sure you're looking for error messages in an error log and that all errors are being reported.  You can add this to the top of your code:

ini_set('display_errors', 1);
	error_reporting(E_ALL);

Link to comment
Share on other sites

Sorry, that's was not heplfull .

the problem is to upload a tif image and do the conversion and then display the image as jpg image in the Thumnaill. thats all ...

 

Link to comment
Share on other sites

I know what you're asking.  I'm trying to figure out what happens when you run the code you showed.  You said that nothing happens, but that's not correct.  Something has to happen when you run code, it's not just going to not do anything.  Either it's going to work, or there's going to be an error.  Those are the 2 options.  If it's not working to save the file, then you need to figure out what the error message says, and the 2 lines I posted will make sure that error messages are being displayed.  Your other option is to figure out where your error log is and look there.

Link to comment
Share on other sites

Is ImageMagick listed in the phpinfo() listing?

Have you actually managed to test ImageMagick to see if it actually works, before producing the converting script?

A simple test script like

<?php
$im = new Imagick();
$im->newPseudoImage(100, 100, "magick:rose");
$im->setImageFormat("png");
$im->roundCorners(5, 3);
$type = $im->getFormat();
header("Content-type: $type");
echo $im->getimageblob();
?>

or

<?php

/* Create a new imagick object */
$im = new Imagick();

/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");

/* Create imagickdraw object */
$draw = new ImagickDraw();

/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);

/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);

/* Close the pattern */
$draw->popPattern();

/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');

/* Set font size to 52 */
$draw->setFontSize(52);

/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");

/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");

/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);

/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);

/* Set the format to PNG */
$canvas->setImageFormat('png');

/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>

 

Link to comment
Share on other sites

Thank you  first for your reply..

i tried this using exec function , and was helpful , but the problem is by saving the uploaded img.

how can make directory by Exec function,.

exec("convert $original_image  $new_image.png ");

 

Link to comment
Share on other sites

Thanks ^_^ !!  Problem solved.

chdir('\path\to\your\directory');
exec("convert $original_image  $new_image.png ");

Yes, we have to change the Directory as you said ...

but now i want to convert from  pdf to png ..

do you have any idea ??

Edited by GleyRejeb
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...