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
https://forums.phpfreaks.com/topic/65668-image-upload/
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
https://forums.phpfreaks.com/topic/65668-image-upload/#findComment-328657
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
https://forums.phpfreaks.com/topic/65668-image-upload/#findComment-328920
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
https://forums.phpfreaks.com/topic/65668-image-upload/#findComment-328933
Share on other sites

Oh boy.. thats due to the server setup.. try to override it

 

change

$im_src = imagecreatefromjpeg($pic_name);

to

set_time_limit ( 0 ); //reset timeout timer
ini_set('memory_limit', '30M'); 
$im_src = imagecreatefromjpeg($pic_name);

 

let hope you don't join GaryDT's thread

 

Link to comment
https://forums.phpfreaks.com/topic/65668-image-upload/#findComment-329299
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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