Jump to content

image upload


ayok

Recommended Posts

Hi guys,

I have this script to resize the image

<?php
function change_pic($pic_name)
{
    header("Content-type:image/jpeg");

    $im_src = imagecreatefromjpeg($pic_name);
    $width_src = imagesx($im_src);
    $height_src = imagesy($im_src);

    $width_dst = 100;
    $height_dst = 100;
    $quality = 60;

    //resize process
    $im = imagecreatetruecolor($width_dst, $height_dst);
    imagecopyresampled($im, $im_src, 0, 0, 0, 0, $width_dst, $height_dst, $width_src,
        $height_src);

    imagejpeg($im, $pic_name, $quality);
    imagedestroy($im_src);
    imagedestroy($im);
}

if ($fupload_type = "image/pjpeg" || $fupload_type = "image/jpeg")
{
    copy($fupload, "./$fupload_name");
    change_pic($fupload_name);
}
else
{
    echo "File must be JPEG";
}?>

 

The codes resize the uploaded pic to 100X100 px from the original pic.

 

However, I want to modify this script so I can resize into two size and put it in different folder. I can put the original pic with the original size in a folder, but how can I have two resized pic like, say, 100X100 and 300X300?

 

Thank you,

 

ayok

Link to comment
Share on other sites

try this

<?php
function change_pic($pic_name, $newname, $width=100, $height=100, $quality=60)
{
//not sure why the head as we're saving to file!
    //header("Content-type:image/jpeg");

    $im_src = imagecreatefromjpeg($pic_name);
    $width_src = imagesx($im_src);
    $height_src = imagesy($im_src);

    $width_dst = $width;
    $height_dst = $height;
    $quality = $quality;

    //resize process
    $im = imagecreatetruecolor($width_dst, $height_dst);
    imagecopyresampled($im, $im_src, 0, 0, 0, 0, $width_dst, $height_dst, $width_src,
        $height_src);

    imagejpeg($im, $newname, $quality);
    imagedestroy($im_src);
    imagedestroy($im);
}

if ($fupload_type = "image/pjpeg" || $fupload_type = "image/jpeg")
{
    copy($fupload, "./$fupload_name");
    change_pic($fupload_name, "./".$fupload_name,300,300);
    change_pic($fupload_name, "./100".$fupload_name,100,100);
}
else
{
    echo "File must be JPEG";
}?>

 

this code will create the 300 sized image and the 100 sized one..

 

Note the file name of the 100 with have 100 prefixed.. as i don't know where you wanted it stored

Link to comment
Share on other sites

I see..

Thanks MadTechie, it works now.

 

However, I still have a question regarding uploading image. The script is working on localhost with IE. It uploads and resizes the picture, but if I test on Opera, it's not uploaded. Do you know why?

 

Thanks,

ayok

Link to comment
Share on other sites

<?php
session_start();
if(!empty($namauser) AND !empty ($passuser))
{
include "../dbconnect.php";
echo "<H3 align=center>Add Picture</H3> 
<FORM enctype=MULTIPART/FORM-DATA METHOD=POST ACTION=input_pic.php>
<table align=center border=0>
<tr><td>Title: </td> <td><INPUT TYPE=TEXT SIZE=60 NAME= title></td></tr>
<tr><td>Category: </td> <td><SELECT NAME=category>
<OPTION VALUE=0 SELECTED>--Choose Category--";
$tampil=mysql_query("SELECT * FROM category ORDER BY id");

while($data=mysql_fetch_array($tampil))
{
	echo "<OPTION VALUE=$data[id]>$data[name]";
}
echo "</OPTION></SELECT></td></tr>
<tr><td>Upload:</td> <td>
<input type=file name=fupload></td></tr>
</table>
<INPUT TYPE=SUBMIT VALUE=Add>

</FORM><a href=foto2.php>Show and delete pictures</a><BR>
<a href=logout.php>logout</a>";
}else{
echo "You have to login before adding new items<BR>";
echo "<a href=form_login.php>Login</a>";
}
?>

<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
}
body {
background-color: #993300;
}
a {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #00FF00;
font-weight: bold;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #FFFF00;
}
a:hover {
text-decoration: underline;
color: #FFFFFF;
}
a:active {
text-decoration: none;
}
-->
</style>

Link to comment
Share on other sites

ok

you need to add some quotes

i think thats all of them!

 

<?php
session_start();
if(!empty($namauser) AND !empty ($passuser))
{
include "../dbconnect.php";
echo "<H3 align=center>Add Picture</H3> 
<FORM enctype='MULTIPART/FORM-DATA' METHOD='POST' ACTION='input_pic.php'>
<table align=center border=0>
<tr><td>Title: </td> <td><INPUT TYPE='TEXT' SIZE=60 NAME='title'></td></tr>
<tr><td>Category: </td> <td><SELECT NAME='category'>
<OPTION VALUE='0' SELECTED>--Choose Category--";
$tampil=mysql_query("SELECT * FROM category ORDER BY id");

while($data=mysql_fetch_array($tampil))
{
	echo "<OPTION VALUE='$data[id]'>$data[name]";
}
echo "</OPTION></SELECT></td></tr>
<tr><td>Upload:</td> <td>
<input type='file' name='fupload'></td></tr>
</table>
<INPUT TYPE='SUBMIT' VALUE='Add'>

</FORM><a href='foto2.php'>Show and delete pictures</a><BR>
<a href='logout.php'>logout</a>";
}else{
echo "You have to login before adding new items<BR>";
echo "<a href='form_login.php'>Login</a>";
}
?>

<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
}
body {
background-color: #993300;
}
a {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #00FF00;
font-weight: bold;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #FFFF00;
}
a:hover {
text-decoration: underline;
color: #FFFFFF;
}
a:active {
text-decoration: none;
}
-->
</styl

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.