Witam. Szukałem odpowiedzi na mój problem m.in tu:
http://forum.gmclan.org/index.php?showtopic=1745 , jednak nie znam się na tyle na PHP, żeby rozwiązanie przekształcić do mojego trochę bardziej złożonego tematu. Otóż w skrócie, mam skrypt adaptujący rozmiar i format obrazka do możliwości danego urządzenia mobilnego i chciałbym, żeby owe powstałe wymiary zostały wpisane do znaczników width="" oraz height="" elementu <img /> w HTML.
Skrócony HTML:
<?php
header("Content-type: application/xhtml+xml"); echo '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
include('D:/Apache Server/htdocs/ia/imageAdaptation.php');
$i='gfx/img_mech.jpg';
$imgurl=convertImage($i);
$imageDimensionWidth=setImageDimension();
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="pojemnik">
<div id="srodek">
<img class="obrazek" src="
<?php echo $imgurl; ?>" alt=""
<?php echo 'width="' . $imageDimensionWidth . '" height="' . $imageDimensionHeight . '"' ?> />
</div>
</div>
</body>
</html>
Z inlude'owanego pliku imageAdaptation.php, który służy do transcodowania obrazków, chciałbym zmeinne $imageDimensionWidth oraz $imageDimension Height, które znajdują się w zwracanej tablicy $dimensionArray przez funkcję setImageDimension.
Kody funkcji setImageDimension:
function setImageDimension($imageWidth,$imageHeight,$deviceWidth,$deviceHeight)
{
$imageAspect=$imageWidth/$imageHeight;
$deviceAspect=$deviceWidth/$deviceHeight;
$imageAspectRatio=$imageAspect;
if (($imageWidth>$deviceWidth)&&($imageHeight>$deviceHeight))// Image larger than the screen
{
if (($imageAspect< 1) && ($deviceAspect< 1)) // both case Height is bigger than the width
{
$device2imageratio=$deviceHeight/$imageHeight;
$imageDimensionHeight=(integer)($imageHeight*$device2imageratio);
$imageDimensionWidth=(integer) ($imageDimensionHeight*$imageAspectRatio);
}
else if (($imageAspect > 1) && ($deviceAspect > 1)) // both case Width is bigger than the width
{
$device2imageratio=$deviceWidth/$imageWidth;
$imageDimensionWidth=(integer)($imageWidth*$device2imageratio);
$imageDimensionHeight=(integer) ($imageDimensionWidth/$imageAspectRatio);
}
else if (($imageAspect < 1) && ($deviceAspect > 1)) // So fit to Height
{
$device2imageratio=$deviceHeight/$imageHeight;
$imageDimensionHeight=(integer)($imageHeight*$device2imageratio);
$imageDimensionWidth=(integer) ($imageDimensionHeight*$imageAspectRatio);
}
else if (($imageAspect > 1) && ($deviceAspect< 1)) // So fit to Width
{
$device2imageratio=$deviceWidth/$imageWidth;
$imageDimensionWidth=(integer)($imageWidth*$device2imageratio);
$imageDimensionHeight=(integer) ($imageDimensionWidth/$imageAspectRatio);
}
else // Fit to anything
{
$device2imageratio=$deviceWidth/$imageWidth;
$imageDimensionWidth=(integer)($imageWidth*$device2imageratio);
$imageDimensionHeight=(integer) ($imageDimensionWidth/$imageAspectRatio);
}
}
else if (($imageWidth< $deviceWidth)&&($imageHeight < $deviceHeight)) // Image smaller than the screen
{
$imageDimensionHeight=$imageHeight;
$imageDimensionWidth=$imageWidth;
}
else // Any one portion of the image is outside the screen
{
if ($imageWidth> $deviceWidth) // So fit width
{
$device2imageratio=$deviceWidth/$imageWidth;
$imageDimensionWidth=(integer)($imageWidth*$device2imageratio*0.9); //dopisane
$imageDimensionHeight=(integer) ($imageDimensionWidth/$imageAspectRatio);
}
else // So fit Height
{
$device2imageratio=$deviceHeight/$imageHeight;
$imageDimensionHeight=(integer)($imageHeight*$device2imageratio);
$imageDimensionWidth=(integer) ($imageDimensionHeight*$imageAspectRatio);
}
}
$dimensionArray=array ($imageDimensionWidth,$imageDimensionHeight); $widthError=0;
$heightError=0;
if($deviceWidth< $imageDimensionWidth)
{
$widthError=1;
}
if($deviceHeight< $imageDimensionHeight){
$heightError=1;
}
return $dimensionArray;
}
Problem jest w tym, że funkcja ta posiada 4 parametry i nie mam pojęcia jak w pliku HTML wywołać tą funkcję, bo jej parametry są zależne od innych funkcji, wyciągnąć i oddzielić $imageDimensionWidth i $imageDimensionHeight z $dimensionArray.
Pełny kod skryptu można pobrać z:
http://www.iwanowicz.home.pl/upload/imageAdaptation.zipDziękuję z góry za cenne wskazówki.