Jump to content

Resized image inserting?


balkan7

Recommended Posts

i need help how to insert into datebase orginal and thumbail photos ?

<?php
if (isset ($_FILES['news_picture'])){
$image_types = array(".gif", ".GIF", ".jpeg", ".JPEG", ".jpg", ".JPG", ".png", ".PNG");
$imgext = strrchr($_FILES['news_picture']['name'], ".");
$news_picture = $_FILES['news_picture']['name'];
$imgsize = $_FILES['news_picture']['size'];
$imgtemp = $_FILES['news_picture']['tmp_name'];
$imgdest = IMAGES."news/".$news_picture;
if (!in_array($imgext, $image_types)) {
	$error = 1;
} else {
	move_uploaded_file($imgtemp, $imgdest);
	chmod($imgdest,0644);
}
$imagepath = $news_picture;
  $save = IMAGES."news/" . $imagepath; //This is the new file you saving
  $file = IMAGES."news/" . $imagepath; //This is the original file

  list($width, $height) = getimagesize($file) ;
  
  $modwidth = 230;
  
  $diff = $width / $modwidth;

  $modheight = $height / $diff; 
  $tn = imagecreatetruecolor($modwidth, $modheight);
  $image = imagecreatefromjpeg($file); 
  imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
  
  imagejpeg($tn, $save, 100) ; 

  $save = IMAGES."news/thumb/thumb_". $imagepath; //This is the new file you saving
  $file = IMAGES."news/". $imagepath; //This is the original file

  list($width, $height) = getimagesize($file) ; 

  $modwidth = 130; 

  $diff = $width / $modwidth;

  $modheight = $height / $diff; 
  $tn = imagecreatetruecolor($modwidth, $modheight) ; 
  $image = imagecreatefromjpeg($file) ; 
  imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

  imagejpeg($tn, $save, 100) ;
  
$result = mysql_query("INSERT INTO news (picture, picture_thumb) values ('$news_picture', 'here must be thumbail')");
}?>

 

plz help

Link to comment
Share on other sites

I am of the mindset that files should be stored in the file system and the links to those files be stored in the DB.

 

Meaning, store the images in a directory such as  /users/profiles/images/

 

Then store the filename and if you want, the path as well, that way your not trying to extract a large image from the DB, you can pull it from the file system and I believe that it is a bit quicker and less server load.

 

 

 

*note to anyone who disagrees*

If I am incorrect about the time and server load factors, please correct me.

 

Nate

Link to comment
Share on other sites

original file has stored in image folder (news) thumbail has stored in image folder (news/thumb/).

 

pasted code working, create original image and thumbail image, now i need command for image name stored in datebase like test.jpg and test_thumb.jpg

 

in datebase have 2 columns:

news_picture

news_picture_thumb

 

i hope can undestand me what i need!

Link to comment
Share on other sites

I would bet that if you echo'd the var $news_picture, you would get the image name that you are looking for.

 

all you have to do is simply insert that into the db. You can either insert just the name of the file or the name of the file with the path as well.

 

You also set the name of the thumb in a Var, so that as well just needs to be inserted into the db. You almost have it, just another step to go and it should work for ya.

 

From what I can tell of the code, the following should work for you. I assume the file name for the original and thumbnail are the same, if thats the case, then the following query should be good to go.

 

<?php
$picture_thumb = 'thumb_'.$news_picture;
$query = "INSERT INTO news (picture, picture_thumb) values ('$news_picture', '$picture_thumb') "
$result = mysql_query($query);
?>

 

Nate

Link to comment
Share on other sites

I am sorry, I looked over the post again and I missed something last time

 

Use the following code instead.

 

<?php
$picture_thumb = 'thumb_'.$news_picture;
$query = "INSERT INTO news (news_picture, news_picture_thumb) values ('$news_picture', '$picture_thumb') "
$result = mysql_query($query);
?>

 

You told me the name of your fields in the DB and I missed that. Try that and lemme know if it works. If it don't work, then change it to the following and post what gets echo'd

 

<?php
$picture_thumb = 'thumb_'.$news_picture;
$query = "INSERT INTO news (picture, picture_thumb) values ('$news_picture', '$picture_thumb') ";
$result = mysql_query($query) or die(mysql_error().'<br><br>'.echo $query; ); 
?>

 

Nate

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.