Jump to content

Recommended Posts

Hello i am quite new with php, and i have designed a small content management system, it uploads images but in a very large scale. I have tried lots of different ways to implement thumbnail script into my code but im getting no where. I have just found this very small function script that makes my images into thumbnails, well its supposed to but im not having any luck. If someone could help me or at least point me in the right direction of getting this working i would extremely appreciate it.

 

here is my code.

Start.php

 

 

<form enctype="multipart/form-data" action="add.php" method="POST">

Name: <input type="text" name="name"><br>

Description: <input type="text" name="description"><br>

Price: <input type="text" name="price"><br>

Photo: <input type="file" name="photo"> <br>

<input type="submit" value="add">

</form>

 

// Connects to the Database



mysql_connect("localhost", "root", "") or die(mysql_error()) ;

mysql_select_db("cars") or die(mysql_error()) ;



//Retrieves data from MySQL

$data = mysql_query("SELECT * FROM cars") or die(mysql_error());



//Puts it into an array

while($info = mysql_fetch_array( $data ))

{



//Outputs the image and other data

Echo "<img src=images/".$info['photo'] ."> <br>";

Echo "<b>Name:</b> ".$info['name'] . "<br> ";

Echo "<b>Description:</b> ".$info['description'] . " <br>";

Echo "<b>Price:</b> ".$info['price'] . " <hr>";

 

 

Add.php

 

//This is the directory where images will be saved

$target = "images/";

$target = $target . basename( $_FILES['photo']['name']);



//This gets all the other information from the form

$name=$_POST['name'];

$description=$_POST['description'];

$price=$_POST['price'];

$pic=($_FILES['photo']['name']);



// Connects to your Database

mysql_connect("localhost", "root", "") or die(mysql_error()) ;

mysql_select_db("cars") or die(mysql_error()) ;



//Writes the information to the database

mysql_query("INSERT INTO cars VALUES ('$name', '$description', '$price', '$pic')") ;



//Writes the photo to the server

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

{



//Tells you if its all ok

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has 



been added to the directory";

}

else {



//Gives and error if its not

echo "Sorry, there was a problem uploading your file.";

} 

 

**********This is my imageresize function which is what i am having problems with getting it to work in with the rest of my scripts this is what i am trying to get working with the other scripts***********

 

function resizeImage($pic,$toWidth,$toHeight){

    

    // Get the original geometry and calculate scales

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

    $xscale=$width/$toWidth;

    $yscale=$height/$toHeight;

    

    // Recalculate new size with default ratio

    if ($yscale>$xscale){

        $new_width = round($width * (1/$yscale));

        $new_height = round($height * (1/$yscale));

    }

    else {

        $new_width = round($width * (1/$xscale));

        $new_height = round($height * (1/$xscale));

    }



    // Resize the original image

    $imageResized = imagecreatetruecolor($new_width, $new_height);

    $imageTmp     = imagecreatefromjpeg ($pic);

    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);



    return $imageResized;

} 

The below is an image resize class I wrote a while ago. With some modification it should work fine for you...

 

image_cls.php

<?php
class image{

function set_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);
} 
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{ $thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w; $thumb_h=$new_h;
} 
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
}else{
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

function set_image_pro($image,$type,$size,$tmp,$query,$url)
{
define ("MAX_SIZE","500");

if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < MAX_SIZE*1024))
  {
  if ($_FILES["image"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["image"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $image . "<br />";
    echo "Type: " . $type . "<br />";
    echo "Size: " . ($size / 1024) . " Kb<br />";

if (file_exists("images/origimg/" . $image))
      {
      echo $image . " already exists. ";
      }
    else
      {
  move_uploaded_file($tmp,"images/origimg/".$image);
  $origim="images/origimg/".$image;
  $thumbim="images/".$image;
  $thumbw="400";
  $thumbh="400";
  $this->set_createthumb($origim,$thumbim,$thumbw,$thumbh);
  $origim="images/".$image;
  $thumbim="images/thumbs/".$image;
  $thumbw="150";
  $thumbh="150";
  $this->set_createthumb($origim,$thumbim,$thumbw,$thumbh);
      echo "Stored in: " . "images/" . $image;
  echo "<h1>Uploaded Successfully</h1>";
  $sql=mysql_query($query);
  echo"<META HTTP-EQUIV=\"Refresh\" Content=\"2;URL=$url\">\n";
      }
    }
  }
else
  {
  echo "Invalid file";
  echo "Type: " . $type;
  }
}

}
?>

 

This is how I initiated the class, but you will need to modify this...

 

require_once ("image_cls.php");

$image=$_FILES["image"]["name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$tmp=$_FILES["image"]["tmp_name"];
$title=$_POST['title'];
$text=$_POST['text'];

$query="INSERT INTO work SET `image`='$image', `title`='$title', `text`='$text'";
$url="displaywork.php";

$image_pro = new image;
$image_pro->set_image_pro($image,$type,$size,$tmp,$query,$url);

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.