/    Sign up×
Community /Pin to ProfileBookmark

display part of image

when i write the line below, according to the width and height I specify, it shrinks or expands etc…. the image. Is there a way to keep the picture itself intact, but according to the width and height I write, it shows only the portion I want? So the image may just appear partially but not distorted. I know that in css I can say background and do this. But what I want is, I need to use this line in a php code, so I dont think I can do this in css. I dont necessarily use the code below. All I want is, to display part of an image without distorting the image. Can you do it with php? If so how?

<img border=0 width=50 height=20 src=”../../pictures/hor5c.jpg”>

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 08.2008 — http://php.net/imagecopy

Look at the example, it's a pretty good starting point. You'll need php with GD support for it to work though.
Copy linkTweet thisAlerts:
@ariellOct 09.2008 — The following code is nothing special, but when you watch it carefully, you immediately get a glimpse how to handle the GD functions mentioned by scragar. In the example, you'll certainly need to replace image names (and pathes), but once you got that straight, you can play around with calling the code directly from a browser window. This script does all the stuff, such as copying, resizing, blending, bla, bla, bla...

[code=php]
<?php // /_tools/xic/overlay/tab_lines_partofbg.php

$imgpath = '../../../img/bg/';

// image usage name
$usagename = 'bg'; // background
if (@$_GET['un'])
$usagename = $_GET['un'];

// corename for image
$corename = 'ovl'; // overlay
if (@$_GET['cn'])
$corename = $_GET['cn'];

// spectrum name
$specname = 'tab'; // table
if (@$_GET['sn'])
$specname = $_GET['sn'];

// source image dimensions, X1
$startXOverlay = 200; // relative to X1(sizeX)
if (@$_GET['sox'])
$startXOverlay = $_GET['sox'];

// source image dimensions, Y1
$startYOverlay = 5; // relative to Y1(sizeY)
if (@$_GET['soy'])
$startYOverlay = $_GET['soy'];

// new image dimensions, X1
$sizeX = 640;
if (@$_GET['w'])
$sizeX = $_GET['w'];

// new image dimensions, Y1
$sizeY = 350;
if (@$_GET['h'])
$sizeY = $_GET['h'];

// figutive "level" to qualify a number
// (to define the distance between vertical lines in the grid)
$units = 0;
if (@$_GET['u'])
$units = $_GET['u'];

// transparancy
$opacity = 70; // 0 = 100% opaque, 127 = 100% transaparent
if (@$_GET['o'])
$opacity = $_GET['o'];

// color
$RGB = array(127,127,192);

if (@$_GET['rgb']) {
$t = explode('-', $_GET['rgb']);
for ($i=0; $i < count($t); $i++)
$RGB[$i] = $t[$i];
} // RGB passed

$bgFile = 'bg_cspace3_blended.png';
if (@$_GET['bgfile'])
$bgFile = $_GET['bgfile'];

// generate an appropriate file name
// @TODO define rules
$newfile = $imgpath.'.'.$usagename.'.'.$corename.'.'.$specname.'-'; // corepart of ilename
$newfile .= 'sox_'.$startXOverlay.'.soy_'.$startYOverlay.'.w_'.$sizeX.'.h_'.$sizeY.'.u_'.$units.'.o_'.$opacity;
$newfile .= '.png';

// create canvas
$img = imagecreatetruecolor($sizeX,$sizeY);
imageantialias($img, true);
imagealphablending($img, true);

// bg image (underneath overlay)
$bgFile = $imgpath.$bgFile;
$bgImage = imagecreatefrompng($bgFile);
$dim = getimagesize($bgFile);
// calculate prospective remainings
// total width BG image - space between start and OVERLAY start - overlay total width
$sizeXRemaining = ($dim[0] - $startXOverlay - $sizeX);
// total height BG image - space between start and OVERLAY start - overlay total height
$sizeYRemaining = ($dim[1] - $startYOverlay - $sizeY);


// copy PART of image read from file onto destination image (the blend)
imagecopyresized(
/*destination image*/ $img,
/*source image*/ $bgImage,
/*destination coords*/ 0, 0,
/*source STARTING coords*/ $startXOverlay, $startYOverlay,
/*destination lengths*/ $sizeX, $sizeY,
/*source lengths*/ $sizeX, $sizeY
);

// draw the blueish areas ion the background image
// one for the chart, one for the title
$background = imagecolorallocatealpha(
/*resource image*/ $img,
/*R*/ $RGB[0],
/*G*/ $RGB[1],
/*B*/ $RGB[2],
/*alpha*/ $opacity
);

// the actual tab
imagefilledrectangle(
/*resource image*/ $img,
/*coords*/ 20, 20,
($sizeX - 20), ($sizeY - 20),
/*(background-)color*/ $background
);

if ($units > 0) { // grid
// the grid...
// color every second line black, the others grey

// define a color for "ONE" and "THEOTHER"
$linecolor1 = imagecolorallocate($img, 149, 160, 151);
$linecolor2 = imagecolorallocate($img, 138, 138, 138);

// get some cushion rules for our table
// figutive maximum on a 100 percent basis
// NOTE: UNITS stands for a picture line, thus 1/1000 px
$maxValue = 8000; // NORMAL (LINES) TABLE
//$maxValue = ($sizeY*1000); // RASTERED BG (-ONLY) TABLE, the more units, the more rastered

for ($i=$units; $i < $maxValue; $i += $units) {

$x1 = 40;
/*
* the more space you need UNDER the table lines, but WITHIN the image,
* the more you need to increase $y1, i.e.:
* $y1 = ( ($sizeY - 120 - ( ($i / $maxValue) * ($sizeY - 160) ) ) );
* ... and you have pleanty of space for a BOLD explanation on what you show on the image,
* for instance, a GRAPH
*/

$y1 = ( ($sizeY - 80 - ( ($i / $maxValue) * ($sizeY - 120) ) ) );
$x2 = ($sizeX - 40);
$y2 = $y1; // take ($sizeY-40) and you have a falling gradiant!

// draw a graphical LINE
imageline(
/*resource image*/ $img,
/*coords*/ $x1, $y1, $x2, $y2,
/*color*/ ($i % (2 * $units)) == 0 ? $linecolor1 : $linecolor2
);
} // all units
} // grid

// flush to browser
header('Content-type: image/png');

if (@$_GET['file'] != '') { // supposed to be written to disk
if (@$_GET['file']=== '1') { // use pre-formed name
$filename = $newfile;
} // use pre-formed name
else { // use passed name
$filename = $imgpath.$_GET['file'].'.png';
} // use passed name
imagepng($img, $filename, 7, NULL);
} // supposed to be written to disk

imagepng($img);

?>
[/code]


P.S. This is tailored for *.png, but it's easy to change that, just see imageXXX functions in GD...

Best from the south.
×

Success!

Help @ketanco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...