this is part of an image upload script i made. I decided to name the rename the images i upload to the time i upload.
date_default_timezone_set('UTC');
$mvTime = date("dmy").(date("H")+5).date("is");
$folder = "images/";
$tmp = imagecreatetruecolor($orig_w, $orig_h);
imagejpeg($tmp, $folder.$mvTime.'.jpg', 100);
now when i upload the image, it saves the image like 051112174700.jpg . its the date in dmyHis format. now i am confused how i show a preview of the image. because the time is changing all the time. thats why i cant use $folder.$mvTime.'jpg' in the tag src.
can any body help me how i can get the image ?
you can use session for temporally save image names
date_default_timezone_set('UTC');
$mvTime = date("dmy").(date("H")+5).date("is");
$folder = "images/";
$tmp = imagecreatetruecolor($orig_w, $orig_h);
imagejpeg($tmp, $folder.$mvTime.'.jpg', 100);
$_SESSION['img_temp_name']=$mvTime.'.jpg';
You know the path of the image already - $folder.$mvTime.'jpg'.
It is not clear from your question but if you want to show a preview immediately after uploading then inside PHP you could do the following:
echo '<img src="'.$folder.$mvTime.'jpg" />";
Alternatively, you will need to come up with a naming convention that allows you to find the image you want later, or store information on the images somewhere (such as in a database).
You defined the image location in the mvTime, so use it like this?
date_default_timezone_set('UTC');
$mvTime = date("dmy").(date("H")+5).date("is");
$folder = "images/";
$tmp = imagecreatetruecolor($orig_w, $orig_h);
imagejpeg($tmp, $folder.$mvTime.'.jpg', 100);
echo '<img src="'.$folder.$mvTime.'.jpg" />'; // <---- added this
Related
Im working on a template for a website that has already more than 50,000 articles and images assigned to every article.
Before now the article image was visible only inside every article, but now I would like to use thumbnails.
I don't have access to modify the upload image form, so the solution should be something like virtual thumbs created from the original images...
What will be the best approach in this case?
Using Mr. Thumb like I advised a simple script to get it working would be
<?php
include './mrthumb.class.php';
// The image you are resizing. Can be a local path as well.
$image = $_GET['i'];
$quality = 100; // percent
// In this example we are resizing the image in proportionate sizes.
// Below we are specifying the MAX width and height.
$width = 100; // Pixels
$height = 130; // Pixels
// Start Mr. Thumb v1.0
$mrthumb = new MrThumb();
// Render the image
$mrthumb->render( $image );
// Resize the image proportionately
// $mrthumb->constrain( $width, $height );
$mrthumb->proportion( $width, $height );
// Finally, output the image to the browser!
// Optionally we can save the image to a destination
// $mrthumb->saveto( $destination, $filename, $quality );
$mrthumb->output( $quality );
// Clean up after you are done! ;)
$mrthumb->clear_cache();
?>
Then save that to your web server along with the mrthumb class and call a thumbnail in your webpage like
<img src="./mrthumb.php?i=images/myimage.jpg" alt="My Image" />
I've finally been able to complete the script for an multiple image resizer, currently it's resizing the original image into 3 other sizes, but I am unable to figure out how to set the original height and width. I have used the getimagesize() but it does not seem to work.
The whole code is here but I don't think it's necessary to post all of it here. http://pastebin.com/UR75tdj3
I have done the following to set each of the images height and width I'd like them to resize into.
$uploadedfile = $_FILES['file']['tmp_name'];
list($width,$height)= getimagesize($uploadedfile);
#large
$largeWidth = 670;
$largeHeight = 330;
$largeTmp = imagecreatetruecolor($largeWidth, $largeHeight);
#medium
$mediumwidth = 330;
$mediumheight = 330;
$mediumTmp = imagecreatetruecolor($mediumWidth,$mediumHeight);
#small
$smallWidth = 327;
$smallHeight = 158;
$smallTmp = imagecreatetruecolor($smallWidth,$smallHeight);
but I wanted to enter the orignal into another folder as well, so I did the following thinking that getimagesize($_FILES['file']['tmp_name']) would return them correctly but it did not.
#original
$originalWidth = $width; //here and
$originalHeight = $height; // here
$originalTmp = imagecreatetruecolor($originalWidth,$originalHeight);
So how do I get the original image height and width as I have tried to do above?
$originalWidth and $originalHeight should return the specific images width & height, but it does not, that is the only issue I am having.
you want to check the size of
$_FILES['file']['tmp_name']
the actull uploaded file as stored on the system
not $_FILES['file']['name'] which is just the filename
I have a simple PHP script that enables users to upload images on a server. I am after some help in displaying these images as thumbnails in an HTMl page and then letting the user click on these thumbnails and then the original picture is displayed in a new page.
Can I use straight HTML for this? Or do I need some combination of javascript or PHP variant?
I know that there are many questions about this on stackoverflow, and I have tried them, but nothing is what I am after.
I would prefferably like the thumbnails be created on the 'fly' rather than me personally having to create each thumbnal when a user uploads an image.
So basically what language should I use to do this? And also can I have some source code if possible?
thanks
Creating thumbnails every time they are requested is a very bad idea - it takes a lot of processing power, which would be easily saved by keeping them around the first time you create them. I would suggest putting the thumbnail creation in the php script that processes the file upload, so that you save the image and its thumbnail to disk at the same time. You can also keep the thumbnail in memory, or wait until the first time it's requested to create it, but either way you cannot re-generate it every time it is requested.
It is possible to use html to change an image's size, by simply setting the width and/or height properties:
<img src='foo.jpg' alt='foo' width='500' height='300'/>
However, this is a bad idea if you aren't certain that the user will later want to view the full-sized image. The reason is that a thumbnail has a smaller filesize than the full image: if the client only wants to view the thumbnail, then you don't want to waste bandwidth (= your money and the client's time) sending them the full image.
As for your interface, you don't need javascript to accomplish that, just html. However, you will need a server-side script to create the thumbnails that your html page links to.
There are plenty of php thumbnail scripts out there.
Either you use a rewriter to still show the original path, but server a php thumbnail version. Or you have to have the url change to something like:
<img src="thumb.php?file=path/to/picture.jpg&size=128" />
Mighty Stuff
phpThumb
Are just such two. Best configured to utilize the cached folder. I use a modified version of the first script at work.
Here's a PHP script you can build appon only works with jpg images.
It will scan through a directory, normalize the image to a decent dimension and also make a thumb on the fly, then on next refresh it wont need to reprocess the image as its already present in the thumbs dir. Hope it helps...
Script placement
Root>
thisscript.php
/images/
someimage.jpg
someimage2.jpg
thisscript.php
<?php
// config section
$path = "./images/";
$thpath = $path."thumbs/";
// end configration
// Open the directory
$do = dir($path);
// now check if the thumb dir is available if not, create it!!
if (!is_dir($thpath)){
mkdir($thpath);
}
$output = '<div>';
while (($file = $do->read()) !== false){
if (is_dir($path.$file)){
continue;
}else{
$info = pathinfo($path.$file);
$fileext = $info['extension'];
if (strtolower($fileext) == 'jpg'){
if (!is_file($thpath.$file)){
//Normalize Super lrg Image to 750x550
thumb_it($path, $file, $path,750,550,99);
//Make Thumb 200x125
thumb_it($path, $file, $thpath,200,125,99);
$output .='<p><img src="'.$thpath.$file.'" title="" alt="" /></p>';
}else{
$output .='<p><img src="'.$thpath.$file.'" title="" alt="" /></p>';
}
}
}
}
$output .='</div>';
echo $output;
//Functions
function thumb_it($dirn, $file, $thumbdir,$rwidth,$rheight,$quality){
set_time_limit(0);
$filename = $dirn.$file;
$thfilename = $thumbdir.preg_replace('/[^a-zA-Z0-9.-]/s', '_', $file);
// get the filename and the thumbernail directory
if (is_file($filename)){
// create the thumbernail from the original picture
$im = #ImageCreateFromJPEG($filename);
if($im==false){return false;}
$width = ImageSx($im); // Original picture width is stored
$height = ImageSy($im); // Original picture height is stored
if (($width < $rwidth) && ($height < $rheight)){
$n_height = $height;
$n_width = $width;
}else{
// saveing the aspect ratio
$aspect_x = $width / $rwidth;
$aspect_y = $height / $rheight;
if ($aspect_x > $aspect_y){
$n_width = $rwidth;
$n_height = $height / $aspect_x;
}else{
$n_height = $rheight;
$n_width = $width / $aspect_y;
}
}
$newimage = imagecreatetruecolor($n_width, $n_height);
// resizing the picture
imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
// writing to file the thumbnail
if(file_exists($thfilename)){chmod($thfilename, 0777);}
Imagejpeg($newimage, $thfilename, $quality);
imagedestroy($newimage);
imagedestroy($im);
}
}
?>
I would be grateful if anybody could help me out with this.
I have a table which stores the filepath of a set of images, e.g col filepath stores values like: ./phpimages/image3.jpg. Note that my images are stored in folder 'phpimages'
Now i want to loop through all the rows in my table and display thumbnails of the images.
Here is my code:
/
*************************Display all records from images table***************/
//create an instance is Image
$objImage = new Image;
$result = $objImage -> listImage();
$num_rows = mysql_num_rows($result);
//echo $num_rows."records in database";
while($row = mysql_fetch_assoc($result)){
$imagepath="'".$row["filepath"]."'"; // note that if i put $imagepath= "dog.jpg", it displays the image just once!
//set mime type content
header('Content-Type: image/jpeg');
//create original image
$image = imagecreatefromjpeg($imagepath);
//get image dimension
$dim=getimagesize($imagepath);
//Set thumb dimension
$thumbw = 100;
$thumbh = 130;
//create empty image
$thumb_image=imagecreatetruecolor($thumbw, $thumbh);
//Resize original image and copy it to thumb image
imagecopyresampled($thumb_image, $image, 0, 0, 0, 0,
$thumbw, $thumbh, $dim[0], $dim[1]);
//display thumb image
imagejpeg($thumb_image);
}
?>
Please can anyone tell me where my error lies? Many thanks for any help provided
Why not just use <img src="">
You can output only one imagejpeg($thumb_image); using this method. If you want to display all thumbnails in a combined image, you should merge your images to one PHP/GD image, and then output that one.
If you would like to output thumbnail images, then I advise you to use the following tool:
http://phpthumb.sourceforge.net/
So when you iterate through your images, you should output an <img src="" /> for each thumbnail.
I'm having an issue using a watermarking script I have. Below is the script:
if (isset($_GET['imgid'])) {
include "../mysql_connect.php";
$imgid = $_GET['imgid'];
$query = "SELECT * FROM img_ref WHERE id='$imgid'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$imagesource = "../ajax_uploads/$row[submitter_id]-uploads/$row[filename]";
} else {
$imagesource = "../ajax_uploads/" . $_GET['path'];
}
$info = pathinfo($imagesource);
$filetype = $info['extension'];
if($filetype == "gif") $image = #imagecreatefromgif($imagesource);
if($filetype == "jpg") $image = #imagecreatefromjpeg($imagesource);
if($filetype == "png") $image = #imagecreatefrompng($imagesource);
if (!$image) die();
$watermark = #imagecreatefrompng('../images/watermark.png');
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($image, true);
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
Now, if I pass a 'path' variable through, everything works fine and the image is displayed correctly as I would imagine. However, when I attempt to pass through an imgid value, and retrieve the path from the database, the image is presented in raw data on the screen - not as an image.
I have tried specifying the headers
header("Content-type: image/jpeg");
However that hasn't helped. It doesn't seem to be an issue with the folder permissions, as if I specify the path name using the reference in the database, it works fine. It seems to be that including that first "if" section seems to break the output, and I'm at a loss why.
Could anyone possibly shed any light on this for me?
Thank you kindly,
Dan
UPDATE
Okay, somehow its started working with having "Header("Content-type: image/jpeg");" placed at the top of the PHP file, so if I go to the file directly and put in the GET id, I get the picture returned - which is good (no idea what changed though).
However I still have an issue in displaying this picture elsewhere it seems. I'm calling the picture using "fancybox", a jquery plugin. When it displays the image, it is still showing the raw data. If I use the path, it displays fine - just for some reason the raw data shows up when using the GET id option. Am still looking into it but thanks for suggestions so far.
It seems using the header
header("Content-type: image/jpeg");
Seemed to work to get it to display as an image - however it did not work with fancybox for some reason. I looked at the API and have sinced forced fancybox to realise it is an image:
$.fancybox({
'href':'processes/process_watermark.php?imgid=' + id,
'type':'image'
});
That makes it display correctly.
Thanks for everyones help - and all the comments reminding me about SQL attacks.
You don't checking fail of mysql_fetch_array maybe query dont result anything?
Maybe path you giving to pathinfo is not correct.
Your debugging script you should have at the top of script error_reporting(E_ALL)
Change $imagesource = "../ajax_uploads/$row[submitter_id]-uploads/$row[filename]"; to $imagesource = "../ajax_uploads/" . $row['submitter_id'] . "-uploads/" . $row['filename'];