How to change the iframe width and height in PHP - php

Can any body tell how to change the width & height of iframe in php.
in database i am saving below format.while i am displaying i want to fixed size.
this is the iframe content
<iframe width="400" height="225" src="http://www.youtube.com/embed/c7ct6pNOvEE?feature=oembed" frameborder="0" allowfullscreen></iframe>
Here width & height changes dynamically based on site.then how to convert the width to 500 & height to 350 in php
Thanks in advance

You should just use CSS
html, body, iframe {
height: 100%;
width: 100%;
}
Edit: Next time you should define your question PROPERLY and add a proper descriotion before you post it.
Anyway, since you have the sizes in the database you can simply use echo's to change the size. First you need to get the width and height from the database, then echo it in your HTML.
<?php
$iframe = html_entity_decode($post['url']);
$newWidth = 610;
$newHeight = 450;
?>
<iframe
src="<?php echo $iframe; ?>"
height="<?php echo $newHeight; ?>"
width="<?php echo $newWidth; ?>"
frameborder="0" allowfullscreen>
</iframe>
Edit v12351524274573523
$iframe = '<iframe width="400" height="225" src="youtube.com/embed/c7ct6pNOvEE?feature=oembed" frameborder="0" allowfullscreen></iframe>';
$src = html_entity_decode($post['url']);
$height = 450;
$width = 610;
// add autoplay
$src = $src . (strstr($src, '?') ? '&': '?') . 'autoplay=1';
$iframe = preg_replace('/src="(.*?)"/i', 'src="' . $src .'"', $iframe);
$iframe = preg_replace('/height="(.*?)"/i', 'height="' . $height .'"', $iframe);
$iframe = preg_replace('/width="(.*?)"/i', 'width="' . $width .'"', $iframe);

Related

php how to replace img style in all images in output

i need to replace in all images in output their style parameters
for example
$output = '<p><img src="" style="float: left; width: 600px; height: 400px">
some text <img src="" style="float: right; width: 600px; height: 300px">';
i need
$output = '<p><img src="" class="alignleft" width="600" height="400"> some text <img src="" class="alignright" width="600" height="300">';
please help
solved
$output = preg_replace_callback('#<img (.+) style="(.+)" />#isU',
function($matches) {
$styles = explode('; ', $matches[2]);
foreach ($styles as $i) {
if (strstr($i, 'width')) $styles['w'] = $i;
if (strstr($i, 'height')) $styles['h'] = $i;
}
$width = $styles['w'];
$width = str_replace('width:', '', $width);
$width = str_replace('px', '', $width);
$height = $styles['h'];
$height = str_replace('height:', '', $height);
$height = str_replace('px', '', $height);
return '<img ' . $matches[1] . ' width="' . $width . '" height="' . $height . '">';
}
, $output);

How to generate svg in folder and force download as svg file using php?

I have generate output as svg in html using php and have to force download it as svg image when i click download as svg. how to generate it as svg image when i click download.
foreach($items as $item){
// echo $item->top;
$top = str_replace('px', '', $item['top']);
$left = str_replace('px', '', $item['left']);
echo '<div style="background-color:#fff;width:333.333px;height:500px;">';
echo '<div style="position:absolute;top:'.$top.';left:'.$left.'">';
$svg_element = $item['svg'];
echo $svg_element;
echo '</div>';
echo '</div>';
}
// output
<div style="background-color:#fff;width:333.333px;height:500px;"><div style="position:absolute;top:235;left:64.0667">
<svg width="198" height="132" viewBox="0 0 99 52" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="none">
<defs><style>#font-face{font-family: 'Twine';src: url("data:application/font-ttf;charset=utf-8;base64,")
<g id="0.38592716854139286"><text fill="#000000" stroke="none" stroke- width="0" stroke-linecap="round" stroke-linejoin="round" x="32" y="39" text-anchor="middle" font-size="40.30555555555556" font-family="Twine" data-textcurve="0" data-itemzoom="1 1" itemzoom="0.5 0.3939393939393939"> <tspan dy="0" x="50%">Hello</tspan></text></g>
For SVG there should not be any div. so i have removed div and created svg with width and height, background color and put the contents in the file save it as .svg extension
$op = '<svg width="'.$template_width.'" height="'.$template_height.'">';
foreach($items as $item){
// echo $item->top;
$top = str_replace('px', '', $item['top']);
$left = str_replace('px', '', $item['left']);
$op .= str_replace('<svg ', '<svg y="'.$top.'" x="'.$left.'" ', $item['svg']);
}
$op .= '</svg>';
$filename = '/front_'.time().'.svg';
file_put_contents(realpath(dirname(__FILE__)).$filename, $op);

PHP find and replace html attribute in string

I get iframe from some api and i hold this iframe in some var.
I want to search for "height" and change his value to something else. the same with "scrolling".
For example:
<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>
After the php function the iframe will be:
we have change the "height" to 600px and "scrolling" to no
<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>
i have solution with this code:
$iframe = preg_replace('/(<*[^>]*height=)"[^>]+"([^>]*>)/', '\1"600"\2', $iframe);
the problem is that after the "preg_replace" run it remove all html attributes after the "height"
Thanks
You can use DOMDocument for it. Something like this:
function changeIframe($html) {
$dom = new DOMDocument;
$dom->loadHTML($html);
$iframes = $dom->getElementsByTagName('iframe');
if (isset($iframes[0])) {
$iframes[0]->setAttribute('height', '600');
$iframes[0]->setAttribute('scrolling', 'no');
return $dom->saveHTML($iframes[0]);
} else {
return false;
}
}
$html = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';
echo changeIframe($html);
With this method you can modify iframe as you want.
Thanks.
An example as you requested:
$str = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';
$str = preg_replace('/height=[\"\'][0-9]+[\"\']/i', 'height="600"', $str);
$str = preg_replace('/scrolling=[\"\']yes[\"\']/i', 'scrolling="no"', $str);
echo $str; // -> '<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>'

How can I resize and crop an image with PHP?

I am looking for some sort of function that will resize an image to 100x100, 200x200 etc but keep the aspect ratio of the uploaded image in tact.
My plan would be to use some sort of cropping, so to crop in the centre of the image after it's height or width reaches 100.
So my logic is a bit like this.
if ( $currentHeight > $currentWidth ) {
//resize width to 100 and crop top and bottom off so it is 100 high
}
elseif ( $currentWidth > $currentHeight ) {
//resize height to 100 and crop left and right off so it is 100 wide
}
In my mind the image would then be 100x100 and it wont look odd!
Does anyone know how I could do this in a tidy function??
You can also use pure css to crop images to fit specific dimensions using clip:rect:
<style>
/*Size of div that contains the dox*/
.crop{
float:left;
position:relative;
width:100px;
height:100px;
margin:0 auto;
border: 2px solid #474747;
}
.crop img{
margin:0;
position:absolute;
top:-25px;
left:-25px;
/* (Top,Right,Bottom,Left) */
clip:rect(25px 125px 125px 25px);
}
</style>
<div class="crop">
<img src="http://static.php.net/www.php.net/images/php.gif" width="200" title="" alt="" />
<div>
<br/>
<div class="crop">
<img src="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5" title="" alt="" />
<div>
See In Action
If you want to crop central part, then I suppose logic for counting coordinates can be such:
1) Count horisontal coordinats of crop area:
$horizontal_center = $current_width/2;
$left = $horizontal_center - $new_width/2;
$right = $horizontal_center + $new_width/2;
2) Count vertical coordinates of crop area:
$vertical_center = $current_height/2;
$top = $vertical_center - $new_height/2;
$bottom = $vertical_center + $new_height/2;
here is my answer - ive written a imagemagick/php function to do this:
stackoverflow.com/questions/20927238/

How to get images size fixed in php?

How do I get images in certain fixed size, example I've attached the image and codes for your review
Please click on this link (image) to understand my question
<img src="<?php echo $picfile; ?>" border="0" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>" align="left" style="border:1px solid black;margin-right:5px;">
You can get image details with
$details = get_image_size($filename).
$details[3] will contain the width and height in html format ready for you to use.
Could possibly try this
img src='$filename' style='width:250px; height: 250px'
allows you to strech image to specific size
to scale you could use
img src='$filename' style='width:auto; height: 250px'
There are a few ways to do this. As you are looping through your images, you want to keep track of the maximum width.
$newImageArray = array();
$maxWidth = 0;
$maxHeight = 0;
$i = 0;
forEach ( $imageArray as $picfile ) {
$newImageArray[$i]['picFile'] = $picfile;
$imgsize = get_image_size($picfile);
$newImageArray[$i]['width'] = $imgsize[0];
if ( $imgsize[0] > $maxWidth ) {
$maxWidth = $imgsize[0];
}
$newImageArray[$i]['height'] = $imgsize[1];
if ( $imgsize[1] > $maxHeight ) {
$maxHeight = $imgsize[1];
}
$1++;
}
forEach ( $newImageArray as $i) { ?>
<img src="<?php echo $picfile; ?>" border="0" width="<?php echo $maxWidth; ?>" height="<?php echo $maxHeight; ?>" align="left" style="border:1px solid black;margin-right:5px;">
<?php
}
Now you shouldn't really be doing this. A CSS based option would work better. You can add a wrapper container with a width on it and set the images to display:block;width:100%; and the images will always fill the space. I will edit with that solution shortly.
This will keep the image within a fixed width, and scale the image to fit inside of the box when it is too large.
HTML:
<div class="imgWrap">
<img src="http://www.captainangry.com/img/1236647133510anonib.jpg">
</div>
CSS:
.imgWrap {
width:100px;
height:100px;
float:left;
}
.imgWrap img {
display:block;
max-width:100%;
}
If you don't want to do any work in PHP then just wrap the image in a div and set overflow:hidden. This assumes that you know the height you want all of your images to be.
<div style="width:<?php echo $imgsize[0]; ?>; height:100px; overflow:hidden; border:1px solid black; margin-right:5px">
<img src="<?php echo $picfile; ?>" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>">
</div>

Categories