Jump to content

problems optimizing images


svgmx5

Recommended Posts

I'm trying to optimize a set of images that are rendered with php. The images are stored on the server and the file path to the images are saved on a database.

 

I'm currently using a script i found online, and every-time i load the page to display the images, all i get are weird characters  instead of images, and the following error message:

 

warning: Cannot modify header information - headers already sent by (output started at /home/content/w/a/r/warbudz10/html/images.php:13) in /home/content/w/a/r/warbudz10/html/images.php on line 315

 

Here is the script that i'm using, i hope someone can help me out with this

 

<?php
                    
                            $images = "images/gallery/"; # Location of small versions
                            
                            $sql = "SELECT * FROM  gallery ORDER BY imageID ASC";
                            $run = mysql_query($sql) or die (mysql_error());
                            $num = mysql_num_rows($run);
                                    
                            while($fetch = mysql_fetch_array($run)){
                                $imageID = $fetch['imageID'];
                                $fileName = $fetch['imagePath'];
                                $imageTitle = $fetch['imageTitle'];

							$img = imagecreatefromjpeg('images/gallery/'.$fileName.'');

							header('Content-Type: image/jpeg');
							imagejpeg($img, null, 75);
							imagedestroy($img);

                        ?>
                        
                        	<div class="galleryImgContainer">
                        
                                <div class="galleryImgWrapper"> 
                                
                                    <a href="<?php echo ' '.$images.$fileName.' ' ;?>" rel="lightbox[roadtrip]">
                                    
                                        <img src="<?php echo ' '.$img.' ' ;?>" width="157" height="127" border="0" />
                                    
                                    </a>
                            
                                </div>
                                
                                <div class="imgTitle">
                                
                                    <h1><a href="#"><?php echo ' '.$imageTitle.' ' ;?></a></h1>
                                
                                </div>
                            
                            </div>
                       	<?php
                            }
                        
                        ?>

Link to comment
https://forums.phpfreaks.com/topic/193552-problems-optimizing-images/
Share on other sites

You cannot output image data directly on a HTML page. You must use an <img src="URL of an image" alt=""> HTML tag for each image you put onto a HTML page. The 'URL of an image' that you put into the src= attribute must be to your .php script that outputs the Content-type: header, followed by the image data.

Someone already answered that -

You must use an <img src="URL of an image" alt=""> HTML tag for each image you put onto a HTML page. The 'URL of an image' that you put into the src= attribute must be to your .php script that outputs the Content-type: header, followed by the image data.

I suspect you might be looking for something like this (simplified example)

<?php

// img_src.php

$id = $_GET['id']; // make GET id db safe

// query db for filename, content type and path based on requested photo id

header('Content-Type: image/jpeg');
echo file_get_contents('path/to/file/filename.jpg')

?>

 

Then call img_get.php for each photo like this to display it

<img src="img_src.php?id=foo" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.