Jump to content

Image functions and filenames.


Venom89

Recommended Posts

Hey everyone.

 

Having a little bit of trouble with using the php image functions and using images I have retrieved from my images database.

 

Basically I am pulling out an image from the database and trying to resize it to fit my needs. Here is the signature of my function:

 

function getImage($image_src, $imageType, $newWidth, $newHeight)

Where $image_src is the image data itself.

 

My problem is that most of the image functions files (e.g. imagecreatefromjpeg) require a filename as a parameter, not the image data itself. Is there any way around this?

 

/**
	@param $image_src The image data itself (pulled from blob in a database)
	@param $image_type The type of the image (e.g. jpeg)
	@param $newWidth The width we are aiming for
	@param $newHeight The height we are aiming for
*/
function getImage($image_src, $imageType, $newWidth, $newHeight)
{

	// Get the original geometry and calculate scales
   		list($width, $height) = getimagesize($img_src); // Won't work needs filename
    		$xscale=$width/$newWidth;
    		$yscale=$height/$newHeight;
    
    		// 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);
    		if ($imageType == "image/jpeg")
    		{
    			$imageTmp     = imagecreatefromjpeg ($image_src);
    		}
    		else //if image is png etc.
    		{
    		}
    		
    		imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    		return $imageResized;

}

Link to comment
https://forums.phpfreaks.com/topic/176771-image-functions-and-filenames/
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.