Jump to content

image resize function


kirkh34

Recommended Posts

No one is going to throw something at you like that. You'd need to take advantage of the GD library. A quick Google search comes up with a few things:

 

http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/ < this one uses OOP from a quick glance

http://www.phptoys.com/e107_plugins/content/content.php?content.46

 

It's something you're just going to have to attempt to do yourself. Those aren't the only two tutorials either, there are a heap out there. Just look around, if you have any more problems let us know.

Link to comment
Share on other sites

Here is something to try, it is what I use, you submit the image from a form in this example, the form field passed in this example is named "logo" and can add a database insert or update at the end depending on your needs

 

<?php
include database connection here

// Process new image
if ($_FILES['logo']['tmp_name']&&$_FILES['logo']['tmp_name']!="") {  
// set memory limit for image
ini_set("memory_limit","32M");
// Check file extension
$checkfile=$_FILES['logo']['name'];
$ext=substr($checkfile, -3);
// Redirect if not correct image type
if (!in_array($ext, array('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG'))) {
	header("Location: redirect to some page if wrong file type");
// Close database connection
	mysql_close($link);
	exit ();
}
// Create new image (300px wide can modify to suit height if that is preferred or set both but not advised)
$file=$_FILES['logo']['tmp_name'];
list($width, $height)=getimagesize($file);
// Scale to width
	$main_width=300;
	$main_height=$height*($main_width/$width);

if ($ext=="jpg"||$ext=="JPG") {
	$image=imagecreatefromjpeg($file);
	$newname="image".time().".jpg"; // using time as part of the name guarantees a unique name for each new image to prevent duplicate naming
}
if ($ext=="gif"||$ext=="GIF") {
	$image=imagecreatefromgif($file);
	$newname="image".time().".gif";
}
if ($ext=="png"||$ext=="PNG") {
	$image=imagecreatefrompng($file);
	$newname="image".time().".png";
}
// Create main image file
$main_image=imagecreatetruecolor($main_width, $main_height);
imagecopyresampled($main_image, $image, 0, 0, 0, 0, $main_width, $main_height, $width, $height);
$cp_file="some/path/".$newname;
$site_file="another/path/".$newname;
if ($ext=="jpg"||$ext=="JPG") {
	imagejpeg($main_image, $cp_file, 100); // 100 here is the max quality setting for jpeg
	imagejpeg($main_image, $site_file, 100);
}
if ($ext=="gif"||$ext=="GIF") {
	imagegif($main_image, $cp_file, 100); // 100 here is the max quality setting for gif
	imagegif($main_image, $site_file, 100);
}
if ($ext=="png"||$ext=="PNG") {
// Turn off alpha blending and set alpha flag
	imagepng($main_image, $cp_file, 9); // 9 here is the max setting for png quality
	imagepng($main_image, $site_file, 9);
}

save file name to database here with mysql insert or update query using variable $newname as the name or you can optionally save path aswell using the variable $cp_file and $site_file
?>

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.