Jump to content

Recommended Posts

Hi

 

Ive basically got 12 images and an empty box.

 

When the user clicks an image it goes in the empty box. However the box it came from still displays the image.

 

Whats the best way to do this? Shall I add the image to a session and use if statements such as if $_SESSION['image1'] exists then dont show in original box but show in other box?

 

Im not sure what the best way is?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/89725-help-with-finding-best-solution/
Share on other sites

Make each of the images a link to the same page, but add a different get variable for each image onto the end of the link. Then, on the page, as the image tags are being printed, check each one against the $_GET variable, and if it isn't the same as the get variable, then output it. finally, when you get to the empty box, check the $_GET variable, and output that image into the box.

best way I see is to put all your images in one directory. Then loop through the directory for the pictures, that way if you want to add more pictures there is no need to go into your code, just upload the picture.

 

This script requires 2 files. you main file and a file to resample the picture. I say use resample because if the picture is too big it will make the page look messed up. And you don't want to put a static picture size in the src tags cause the picture will look distorted.

 

here is the main page name it anything you like

<?php
$self = $_SERVER['PHP_SELF'];
// set max picture size here
$maxsize = 800;
// set image directory here
$dir = "images/";
echo "<table width=1000 align=center>
  <tr>\n";
if ($handle = opendir($dir)) {
    /* This is the correct way to loop over the directory. */
echo "<td width=200>\n";
    while (false !== ($file = readdir($handle))) {
      // this will look for all .jpg files
      if(strrchr($file, ".") == ".jpg"){
      echo "<a href=\"$self?pic=$file\">$file</a><br />\n";
      }
    }
echo "</td>\n";
}
echo "<td width=800 align=center valign=middle>\n";
if(isset($_GET['pic'])){
$picture = $_GET['pic'];
echo "<img src=\"imageresize.php?maxsize=$maxsize&source=".$dir.$picture."\" >\n";
} else {
echo " ";
}
echo "</td>
</tr>
</table>\n";
?>

 

and here is the resample file. Name it imageresize.php

<?php
/**********************************
*  Will resize an image to a      *
*  max width or height and keep   *
*  aspect ratio Name this file    *
*  anything you like.             *
*  I will use imageresize.php     *
**********************************/

header('Content-type: image/jpeg');
function resampleimage($maxsize, $sourcefile, $imgcomp=0)
   {
   $g_imgcomp=100-$imgcomp;
   if(file_exists($sourcefile))
       {
       $g_is=getimagesize($sourcefile);
       if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
           $new_width=$g_is[0];
           $new_height=$g_is[1];
       } else {
       $w_adjust = ($maxsize / $g_is[0]);
       $h_adjust = ($maxsize / $g_is[1]);
        if($w_adjust <= $h_adjust)
        {
        $new_width=($g_is[0]*$w_adjust);
        $new_height=($g_is[1]*$w_adjust);
        }
        else
        {
        $new_width=($g_is[0]*$h_adjust);
        $new_height=($g_is[1]*$h_adjust);
        }
       }
       	//SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
    $image_type = strrchr($sourcefile, ".");

    //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
	switch($image_type) {
		case '.jpg':
			$img_src = imagecreatefromjpeg($sourcefile);
			break;
		case '.png':
			$img_src = imagecreatefrompng($sourcefile);
			break;
		case '.gif':
			$img_src = imagecreatefromgif($sourcefile);
			break;
		default:
			echo("Error Invalid Image Type");
			die;
			break;
		}
       $img_dst=imagecreatetruecolor($new_width,$new_height);
       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
       imagejpeg($img_dst);
       imagedestroy($img_dst);
       imagedestroy($img_src);
       return true;
       }
       else
       return false;
   }
resampleimage($_GET['maxsize'], $_GET['source']);
?>

 

Hope that helps

 

Ray

Thanks for the reply, however I am not looking at uploading any images.

 

I have 12 images and I am using ajax to drop the images into a basket.

 

Im just trying to work out the best way to calculate whats in the basket and what isnt and how to show this within the original box (where the 12 images are) and the basket?

 

Sorry if my first post wasnt very clear.

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.