Jump to content

Please help with auto image resize in PHP?


angel1987

Recommended Posts

I have been searching since past 3 days and came across many functions on php.net and other sites but i am unable to get it to work. Because in my case already uploaded image names are coming out of database through a while loop. So not sure how to integrate those functions here.

 

Its a very simple process, images are uploaded and stored in a gallery folder and their names are stored in database.

 

Then these images are displayed through while loop as follows

 

<?php
$sql = "SELECT * FROM gallery";
$result = @mysql_query($sql) or die(mysql_error());
while($rows = @mysql_fetch_array($result)){
$file = $rows['filename'];
?>
<img src="gallery/<?php echo $file; ?>" border="0" />
<br />
<?php
}
?>

 

But image dimension can be random so i want it to show in a fixed size of 150x150. I am looking for a very simple method to make that happen. Please help.

Link to comment
Share on other sites

Please use below function before after uploading image for resize it. After resizing successfully, save thumb image in Database also.

 

function createthumb($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg/',$system[1])){
	$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png/',$system[1])){
	$src_img=imagecreatefrompng($name);
}

Link to comment
Share on other sites

@ litebearer I have came across many such scripts from other sites, but they didn't work.

 

About the link you gave, i tried it but it does not show any image. When i check the source it shows this...

 

<img src="scripts/phpThumb.php?src=gallery/916839599703125000image1.jpg&w=100">

 

 

The image path is correct but image is not displayed. It only works normally like in the code i gave above.

Where am i going wrong?

 

@ chintansshah Images are already uploaded, so i am looking for a simple method to do it on the fly.

 

Link to comment
Share on other sites

I presume..

 

1. you have a folder named 'scripts'

2. in that folder you have a file named 'phpThumb.php'

3. you have a folder named 'allery'

4. in that folder you have an image named '916839599703125000image1.jpg'

 

There also, inconjunction with the above, be a need to 'adjust' the path to the image.

 

Link to comment
Share on other sites

as an aside...

I still recommend

 

1. create folder called thumbs

2. use a simple function (only need to do this one time) that will take all of the images already uploaded and create thumbs of them.

3. add a thumb-creatation function to your upload script

 

The savings on time and cpu usgae will be greatly increased

Link to comment
Share on other sites

Ok i tried it. But for resizing while uploading and storing filenames in database i am trying the script on this website.

 

http://www.plus2net.com/php_tutorial/php_thumbnail.php

 

And it works partially, it can copy the image on server and also store all the data in database, but it just won't create thumbnails.

 

From that script, it is giving this error

 

if (!($_FILES[userfile][type] =="image/pjpeg" OR $_FILES[userfile][type]=="image/gif")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";

 

 

I am uploading jpg files. Is there any different between jpg and jpeg and JPG and JPEG? Some images shows jpg and some shows jpeg as extension and i played with extension but still it won't work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.